home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / mstinterp.c < prev    next >
C/C++ Source or Header  |  1991-09-12  |  103KB  |  3,744 lines

  1. /***********************************************************************
  2.  *
  3.  *    Byte Code Interpreter Module.
  4.  *
  5.  *    Interprets the compiled bytecodes of a method.
  6.  *
  7.  ***********************************************************************/
  8.  
  9. /***********************************************************************
  10.  *
  11.  * Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  12.  * Written by Steve Byrne.
  13.  *
  14.  * This file is part of GNU Smalltalk.
  15.  *
  16.  * GNU Smalltalk is free software; you can redistribute it and/or modify it
  17.  * under the terms of the GNU General Public License as published by the Free
  18.  * Software Foundation; either version 1, or (at your option) any later 
  19.  * version.
  20.  * 
  21.  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  22.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  23.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  24.  * more details.
  25.  * 
  26.  * You should have received a copy of the GNU General Public License along with
  27.  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  28.  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  29.  *
  30.  ***********************************************************************/
  31.  
  32. /*
  33.  *    Change Log
  34.  * ============================================================================
  35.  * Author      Date       Change
  36.  * sbb         12 Sep 91      Fixed some process/gc related lossages.  Poor use of
  37.  *              prepareToStore.
  38.  *
  39.  * sbyrne    20 May 90      Improved error handling when error: or
  40.  *              doesNotUnderstand: occurs.  Also, added ^C handling
  41.  *              to abort execution.
  42.  *
  43.  * sbyrne    24 Apr 90      Improved error handling for fopen/popen primitives.
  44.  *
  45.  * sbyrne    20 Apr 90      Make fileIn not close the stream that it's reading
  46.  *              from; this is taken care of by the caller, and causes
  47.  *              very strange behavior if we try to close it twice!
  48.  *
  49.  * sbyrne     7 Apr 90      Added declaration tracing primitive.
  50.  *
  51.  * sbyrne     7 Apr 90      Fixed fileIn: to check for existence of the file
  52.  *              before trying to open it.  Returns failure if the
  53.  *              file cannot be accessed.
  54.  *
  55.  * sbyrne    25 Mar 90      Minor change for AtariSt: decrease size of ASYNC
  56.  *              queue size.
  57.  *
  58.  * sbyrne    19 Dec 89      Added suport for primitive filein (for use with
  59.  *              autoload --
  60.  *                              "12 gauge autoloader"
  61.  *                              A. Swartzenegger
  62.  *                              The Terminator)
  63.  *
  64.  * sbyrne     2 Sep 89      Process primitives in and working...starting to
  65.  *              switch to compiled methods with descriptor instance
  66.  *              variable in addition to header.
  67.  *
  68.  * sbyrne     9 Aug 89      Conversion completed.  Performance now 40k
  69.  *              bytecodes/sec; was 43k bytecodes/sec.
  70.  *
  71.  * sbyrne    18 Jul 89      Began conversion from stack based method contexts and
  72.  *              blocks to more traditional method contexts and
  73.  *              blocks.  This change was done 1) to make call in from
  74.  *              C easier, 2) to make processs possible (they could
  75.  *              have been implemented using stack based contexts, but
  76.  *              somewhat space-wastefully), and 3) to conform with
  77.  *              the more traditional definition method contexts and
  78.  *              block contexts.
  79.  *
  80.  * sbyrne    26 May 89      added method cache!  Why didn't I spend the 1/2 hour
  81.  *              sooner?
  82.  *
  83.  * sbyrne     7 Jan 89      Created.
  84.  *
  85.  */
  86.  
  87.  
  88. #include "mst.h"
  89. #include "mstinterp.h"
  90. #include "mstdict.h"
  91. #include "mstsym.h"
  92. #include "mstoop.h"
  93. #include "mstsave.h"
  94. #include "mstcomp.h"
  95. #include "mstcint.h"
  96. #include "mstsysdep.h"
  97. #include <math.h>
  98. #include <stdio.h>
  99. #include <signal.h>
  100. #include <sys/time.h>
  101. #include <sys/types.h>
  102. #include <sys/stat.h>
  103. #include <setjmp.h>
  104.  
  105. #define    METHOD_CACHE_SIZE        (1 << 11) /* 2048, mostly random choice */
  106. #ifdef atarist
  107. #define ASYNC_QUEUE_SIZE        16 /* still way too much */
  108. #else
  109. #define ASYNC_QUEUE_SIZE        100 /* way too much */
  110. #endif
  111.  
  112. /* Don't enable this...it doesn't really work properly with the new GC, since
  113.    using local register variables defeats the ability to copy the root set
  114.    when a GC flip occurs. */
  115. /*#define LOCAL_REGS */
  116.  
  117. /* Enabling this means that some accessors for method object pieces, such
  118.    as instance variables or literals, are implemented as routines, instead
  119.    of being in-line code via macros */
  120. /* #define ACCESSOR_DEBUGGING */
  121.  
  122. #ifdef LOCAL_REGS
  123. #define exportSP()    *spAddr = sp
  124. #define exportIP()    *ipAddr = ip
  125. #define exportRegs()    { exportSP(); exportIP(); }
  126. #define importSP()    sp = *spAddr
  127. #define importIP()    ip = *ipAddr
  128. #define importRegs()    { importSP(); importIP(); }
  129. #else
  130. #define exportSP()
  131. #define exportIP()
  132. #define exportRegs()
  133. #define importSP()
  134. #define importIP()
  135. #define importRegs()
  136. #endif /* LOCAL_REGS */
  137.  
  138. #ifndef ACCESSOR_DEBUGGING
  139.  
  140. #define receiverVariableInternal(receiver, index) \
  141.   (!inBounds(receiver, index) && errorf("Index out of bounds %d", index), \
  142.     oopToObj(receiver)->data[index])
  143.  
  144. #define getStackReceiverInternal(numArgs) \
  145.   (stackAt(numArgs))
  146.  
  147. #define methodTemporaryInternal(index) \
  148.   (temporaries[index])
  149.  
  150.  
  151. #define methodLiteralInternal(methodOOP, index) \
  152.   (((Method)oopToObj(methodOOP))->literals[index])
  153.  
  154. #define methodVariableInternal(methodOOP, index) \
  155.   (associationValue(((Method)oopToObj(methodOOP))->literals[index]))
  156.  
  157. #define getMethodByteCodesInternal(methodOOP) \
  158.   (isNil(methodOOP) ? (Byte *)nil \
  159.    : ((Byte *)&((Method)oopToObj(methodOOP))->literals[((Method)oopToObj(methodOOP))->header.numLiterals]))
  160.  
  161.  
  162. #define getMethodHeaderInternal(methodOOP) \
  163.   (((Method)oopToObj(methodOOP))->header)
  164.  
  165. #define getMethodClassInternal(methodOOP) \
  166.   (associationValue(((Method)oopToObj(methodOOP))->literals[((Method)oopToObj(methodOOP))->header.numLiterals - 1]))
  167.  
  168.  
  169. #define storeReceiverVariableInternal(receiver, index, oop) \
  170. {  \
  171.   OOP __storeRecVarOOP = (oop); \
  172.   if (!inBounds(receiver, index)) { \
  173.     errorf("Index out of bounds %d", index); \
  174.   } \
  175.   prepareToStore(receiver, __storeRecVarOOP); \
  176.   oopToObj(receiver)->data[index] = __storeRecVarOOP; \
  177. }
  178.  
  179. #define storeMethodTemporaryInternal(index, oop) \
  180. { \
  181.   OOP __storeMethTempOOP = (oop); \
  182.   prepareToStore(thisContextOOP, __storeMethTempOOP); \
  183.   temporaries[index] = __storeMethTempOOP; \
  184. }
  185.  
  186. #define storeMethodVariableInternal(methodOOP, index, oop) \
  187.   setAssociationValue(((Method)oopToObj(methodOOP))->literals[index], oop)
  188.  
  189. #define storeMethodLiteralInternal(methodOOP, index, oop) \
  190. { \
  191.   OOP __storeMethLitOOP = (oop); \
  192.   prepareToStore(methodOOP, __storeMethLitOOP); \
  193.   ((Method)oopToObj(methodOOP))->literals[index] = __storeMethLitOOP; \
  194. }
  195.  
  196. #define inBoundsInternal(oop, index) \
  197.   ((index) >= 0 && (index) < numOOPs(oopToObj(oop)))
  198.  
  199. #define isBlockContextInternal(contextOOP) \
  200.   (oopClass(contextOOP) == blockContextClass)
  201.  
  202.  
  203. #define receiverVariable        receiverVariableInternal
  204. #define getStackReceiver        getStackReceiverInternal
  205. #define methodTemporary            methodTemporaryInternal
  206. #define methodVariable            methodVariableInternal
  207. #define getMethodByteCodes        getMethodByteCodesInternal
  208. #define getMethodClass            getMethodClassInternal
  209. #define storeReceiverVariable        storeReceiverVariableInternal
  210. #define storeMethodTemporary        storeMethodTemporaryInternal
  211. #define storeMethodVariable        storeMethodVariableInternal
  212. #define inBounds            inBoundsInternal
  213. #define isBlockContext            isBlockContextInternal
  214.  
  215. #define methodLiteral            methodLiteralInternal
  216. #define getMethodHeader            getMethodHeaderInternal
  217. #define storeMethodLiteral        storeMethodLiteralInternal
  218. #endif /* !ACCESSOR_DEBUGGING */
  219.  
  220.  
  221.  
  222. typedef enum {
  223.   openFilePrim,
  224.   closeFilePrim,
  225.   getCharPrim,
  226.   putCharPrim,
  227.   seekPrim,
  228.   tellPrim,
  229.   eofPrim,
  230.   popenFilePrim,
  231.   sizePrim
  232. } filePrimitiveTypes;
  233.  
  234. typedef struct FileStreamStruct {
  235.   OOP        collection;
  236.   OOP        ptr;
  237.   OOP        endPtr;
  238.   OOP        maxSize;
  239.   OOP        file;
  240.   OOP        name;
  241. } *FileStream;
  242.  
  243. typedef struct CompiledMethodStruct *Method;
  244.  
  245. typedef struct MethodContextStruct {
  246.   OBJ_HEADER;
  247.   OOP        sender;
  248.   OOP        ipOffset;    /* an integer byte index into method */
  249.   OOP        spOffset;    /* an integer index into cur context stack */
  250.   OOP        method;        /* the method that we're executing */
  251.   OOP        hasBlock;    /* nil or not nil */
  252.   OOP        selector;    /* the selector that invoked this method */
  253.   OOP        receiver;    /* the Receiver OOP */
  254.   OOP        contextStack[CONTEXT_STACK_SIZE];
  255. } *MethodContext;
  256.  
  257. typedef struct BlockContextStruct {
  258.   OBJ_HEADER;
  259.   OOP        caller;
  260.   OOP        ipOffset;    /* an integer byte index into method */
  261.   OOP        spOffset;    /* an integer index into cur context stack */
  262.   OOP        numArgs;    /* number of arguments we have */
  263.   OOP        initialIP;    /* initial value of IP (an offset) */
  264.   OOP        selector;    /* the selector that invoked this block */
  265.   OOP        home;        /* the home context */
  266.   OOP        contextStack[CONTEXT_STACK_SIZE];
  267. } *BlockContext;
  268.  
  269. typedef struct SemaphoreStruct {
  270.   OBJ_HEADER;
  271.   OOP        firstLink;
  272.   OOP        lastLink;
  273.   OOP        signals;
  274. } *Semaphore;
  275.  
  276. typedef struct ProcessStruct {
  277.   OBJ_HEADER;
  278.   OOP        nextLink;
  279.   OOP        suspendedContext;
  280.   OOP        priority;
  281.   OOP        myList;
  282. } *Process;
  283.  
  284. typedef struct ProcessorSchedulerStruct {
  285.   OBJ_HEADER;
  286.   OOP        processLists;
  287.   OOP        activeProcess;
  288. } *ProcessorScheduler;
  289.  
  290. long            byteCodeCounter;
  291. long            cacheHits = 0;
  292. long            cacheMisses = 0;
  293.  
  294. /* If this is true, for each byte code that is executed, the byte index
  295.  * within the current CompiledMethod and a decoded interpretation of
  296.  * the byte code is printed on standard output. */
  297. Boolean            executionTracing;
  298.  
  299. /* When this is true, and an interrupt occurs (such as SIGSEGV), Smalltalk
  300.  * will terminate itself by making a core dump (normally it does not
  301.  * terminate in this manner). */
  302. Boolean            makeCoreFile = false;
  303.  
  304.  
  305. Byte            *ip, **ipAddr = &ip;
  306. OOP            *sp, **spAddr = &sp;
  307. OOP            thisMethod;
  308.  
  309. int            collide[METHOD_CACHE_SIZE];
  310.  
  311. #ifdef countingByteCodes
  312. static long        byteCodes[256];
  313. static long        primitives[256];
  314. #endif
  315.  
  316. static OOP        methodCacheSelectors    [METHOD_CACHE_SIZE];
  317. static OOP        primitiveCacheSelectors [METHOD_CACHE_SIZE];
  318. static OOP        methodCacheClasses      [METHOD_CACHE_SIZE];
  319. static OOP        primitiveCacheClasses   [METHOD_CACHE_SIZE];
  320. static OOP        methodCacheMethods      [METHOD_CACHE_SIZE];
  321. static int        primitiveCachePrimitives[METHOD_CACHE_SIZE];
  322.  
  323. static OOP        queuedAsyncSignals    [ASYNC_QUEUE_SIZE]; 
  324. static int        asyncQueueIndex;
  325. static OOP        switchToProcess; /* non-nil when proc switch wanted */
  326.  
  327. static OOP        *temporaries;    /* points into method or block context
  328.                      * to start of arguments and
  329.                      * temporaries */
  330. static OOP        self;
  331. static OOP        thisContextOOP;
  332. static Boolean        inInterpreter = false;
  333. static int        exceptFlag;
  334.  
  335. /* When true, this causes the byte code interpeter to immediately act
  336.  * as if it saw a stream af method return bytecodes, until it finally exits.
  337.  */
  338. static Boolean        abortExecution = false;
  339.  
  340. /* When this is true, it means that the system is executing external C code,
  341.  * which can be used by the ^C handler to know whether it longjmp to the
  342.  * end of the C callout primitive in executePrimitiveOperation. */
  343. static Boolean        inCCode = false;
  344.  
  345.  
  346. /* Used to handle the case when the user types a ^C while executing callout
  347.  * code */
  348. static jmp_buf cCalloutJmpBuf;
  349.  
  350. /* when this flag is on and execution tracing is in effect, the top
  351.  * of the stack is printed as well as the byte code */
  352. static Boolean        verboseExecTracing = false;
  353.  
  354.  
  355. #ifdef ACCESSOR_DEBUGGING
  356. static OOP        methodTemporary(), receiverVariable(),
  357.               methodVariable(), getMethodClass(),
  358.             getStackReceiver(), methodLiteral();
  359. static void        storeMethodTemporary(), storeReceiverVariable(),
  360.               storeMethodVariable(), storeMethodLiteral();
  361. static Boolean        inBounds(), isBlockContext();
  362. static Byte        *getMethodByteCodes();
  363. static MethodHeader    getMethodHeader();
  364. #endif /* ACCESSOR_DEBUGGING */
  365. static OOP        findMethod(), createMethodContext(), 
  366.             getMethodContext(), getActiveProcess(),
  367.             getProcessLists(), highestPriorityProcess(),
  368.             removeFirstLink(), semaphoreNew();
  369. static void        returnWithValue(), 
  370.             sendBlockValue(),
  371.             showBacktrace(),
  372.             invalidateMethodCache(), methodHasBlockContext(),
  373.             sleepProcess(), resumeProcess(), activateProcess(),
  374.             changeProcessContext(), addLastLink(),
  375.             suspendActiveProcess(), moveSemaphoreOOPs();
  376. static Boolean        executePrimitiveOperation(),
  377.             noParentContext(), isEmpty(), isRealOOP();
  378. static char        *selectorAsString();
  379. static signalType    interruptHandler(), stopExecuting();
  380.  
  381. static OOP        *mathSelectors[16] = {
  382.   &plusSymbol,            /* 0  + */
  383.   &minusSymbol,            /* 1  - */
  384.   &lessThanSymbol,        /* 2  < */
  385.   &greaterThanSymbol,        /* 3  > */
  386.   &lessEqualSymbol,        /* 4  <= */
  387.   &greaterEqualSymbol,        /* 5  >= */
  388.   &equalSymbol,            /* 6  = */
  389.   ¬EqualSymbol,        /* 7  ~= */
  390.   ×Symbol,            /* 8  * */
  391.   ÷Symbol,        /* 9  / */
  392.   &remainderSymbol,        /* 10 \\ */
  393.   &plusSymbol,            /* 11 @, not implemented */
  394.   &bitShiftColonSymbol,        /* 12 bitShift: */
  395.   &integerDivideSymbol,        /* 13 // */
  396.   &bitAndColonSymbol,        /* 14 bitAnd: */
  397.   &bitOrColonSymbol        /* 15 bitOr: */
  398. };
  399.  
  400. struct SpecialSelectorStruct {
  401.   OOP        *selector;
  402.   int        args;
  403. } specialMessages[16] = {
  404.   &atColonSymbol,        1,
  405.   &atColonPutColonSymbol,    2,
  406.   &sizeSymbol,            0,
  407.   &nextSymbol,            0,
  408.   &nextPutColonSymbol,        1,
  409.   &atEndSymbol,            0,
  410.   &sameObjectSymbol,        1,
  411.   &classSymbol,            0,
  412.   &blockCopyColonSymbol,    1,
  413.   &valueSymbol,            0,
  414.   &valueColonSymbol,        1,
  415.   &doColonSymbol,        1,
  416.   &newSymbol,            0,
  417.   &newColonSymbol,        1,
  418.   &nilSymbol,            0, /* unimplemented selector */
  419.   &nilSymbol,            0  /* unimplemented selector */
  420. };
  421.  
  422. /*
  423.  
  424. ### This is from the old stack based context days...update it to reflect
  425. reality!
  426.  
  427. +-----------------------------------+
  428. | receiver (self)            |
  429. +-----------------------------------+
  430. | args                    |
  431. +-----------------------------------+
  432. | ...                    |
  433. +-----------------------------------+
  434. | temps                    |
  435. +-----------------------------------+
  436. | ...                    |
  437. +-----------------------------------+
  438. | saved ip of caller (relative)        | FP, SP on interpreter entry
  439. +-----------------------------------+
  440. | saved method of caller            |
  441. +-----------------------------------+
  442. | saved temp pointer of caller        |
  443. +-----------------------------------+
  444. | saved frame pointer of caller (?) |
  445. +-----------------------------------+
  446. | isBlock (boolean)                 |
  447. +-----------------------------------+
  448. | method context pointer        |
  449. +-----------------------------------+
  450. |                                   | SP (after saving state)
  451.  
  452.  */
  453.  
  454.  
  455. /*
  456.  Interpretation of the virtual machine byte codes
  457.  
  458. 0-15 push receiver variable     0000iiii
  459. 16-31 push temporary location    0001iiii
  460. 32-63 push literal constant    001iiiii
  461. 64-95 push literal variable    010iiiii
  462. 96-103 pop & store rec var    01100iii
  463. 104-111 pop & store temp loc    01101iii
  464. 112-119 push indexed        01110iii receiver true false nil -1 0 1 2
  465. 120-123 return indexed        011110ii receiver true false nil
  466. 124-125 return st top from    0111110i message, block
  467. 126-127 unused            0111111i
  468. 128    push indir        10000000 jjkkkkkk (receiver var, temp loc,
  469.                            lit const, lit var)
  470.                            [jj] #kkkkkk
  471. 129    store indir        10000001 jjkkkkkk (rv, tl, illegal, lv)
  472. 130    pop & store indir    10000010 jjkkkkkk (like store indir)
  473. 131    send lit selector    10000011 jjjkkkkk sel #kkkkk with jjj args
  474. 132    send lit selector    10000100 jjjjjjjj kkkkkkkk (as 131)
  475. 133    send lit sel to super    10000101 jjjkkkkk as 131
  476. 134    send lit to super    10000110 jjjjjjjj kkkkkkkk like 132
  477. 135    pop stack top        10000111
  478. 136    duplicate stack top    10001000
  479. 137    push active context    10001001
  480. 138-143    unused
  481. 144-151    jmp iii+1        10010iii
  482. 152-159    pop & jmp false iii+1    10011iii
  483. 160-167    jmp (iii-4)*256+jjjjjjjj10100iii jjjjjjjj
  484. 168-171 pop & jmp on true    101010ii jjjjjjjj ii*256+jjjjjjjj
  485. 172-175 pop & jmp on false    101011ii jjjjjjjj like 168
  486. 176-191 send arith message    1011iiii
  487. 192-207    send special message    1100iiii
  488. 208-223 send lit sel #iiii    1101iiii with no arguments
  489. 224-239 send lit sel #iiii    1110iiii with 1 argument
  490. 240-255 send lit sel #iiii    1111iiii with 2 arguments
  491. */
  492.  
  493. /*
  494.  *
  495.  * How the interpreter works:
  496.  *  1) The interpreter expects to be called in an environment where there
  497.  *     already exists a well-defined method context.  The instruction pointer,
  498.  *     stored in the global variable "ip", and the stack pointer, stored in the
  499.  *     global variable "sp", should be set up to point into the current
  500.  *     method and MethodContext.  Other global variables, such as "thisMethod",
  501.  *     "self", "temporaries", etc. should also be setup.  See the routine
  502.  *     prepareExecutionEnvironment for details.
  503.  *  2) The interpreter checks to see if any change in its state is required,
  504.  *     such as switching to a new process, dealing with an asynchronous signal
  505.  *     which is not yet implemented, and printing out the byte codes that are 
  506.  *     being executed, if that was requested by the user.
  507.  *  3) After that, the byte code that ip points to is fetched and decoded.
  508.  *     Some byte codes perform jumps, which are performed by merely adjusting
  509.  *     the value of ip.  Some are message sends, which are described in
  510.  *     more detail below.  Some instructions require more than one byte code
  511.  *     to perform their work; ip is advanced as needed and the extension
  512.  *     byte codes are fetched.
  513.  *  4) After dispatching the byte code, the interpreter loops around to
  514.  *     execute another byte code.  If ip has changed to point to nil, it is
  515.  *     a signal that the execution of the method is over, and the interpreter
  516.  *     returns to its caller.
  517.  *
  518.  * Note that the interpreter is not called recursively to implement message
  519.  * sends.  Rather the state of the interpreter is saved away in the currently
  520.  * executing context, and a new context is created and the global variables
  521.  * such as ip, sp, and temporaries are initialized accordingly.
  522.  *
  523.  * When a message send occurs, the sendMessage routine is invoked.  It 
  524.  * determines the class of the receiver, and checks to see if it already has
  525.  * cached the method definition for the given selector and receiver class.
  526.  * If so, that method is used, and if not, the receiver's method dictionary
  527.  * is searched for a method with the proper selector.  If it's not found in
  528.  * that method dictionary, the method dictionary of the classes parent is
  529.  * examined, and on up the hierarchy, until a matching selector is found.
  530.  *
  531.  * If no selector is found, the receiver is sent a #doesNotUnderstand: message
  532.  * to indicate that a matching method could not be found.
  533.  *
  534.  * If a method is found, it is examined for some special cases.  The special
  535.  * cases are primitive return of self, return of an instance variable, or
  536.  * execution of a primitive method definition.  This latter operation is
  537.  * performed by the executePrimitiveOperation routine.  If the execution
  538.  * of this primitive interpreter fails, the normal message send operation
  539.  * is performed.
  540.  *
  541.  * If the found method is not one of the special cases, or if it is a 
  542.  * primitive that failed to execute, a "normal" message send is performed.
  543.  * This basically entails saving away what state the interpreter has, such
  544.  * as the values of ip, and sp, being careful to save their relative locations
  545.  * and not their physical addresses, because one or more garbage collections
  546.  * could occur before the method context is returned to, and the absolute
  547.  * pointers would be invalid.
  548.  *
  549.  * The sendMessage routine then creates a new MethodContext object, makes
  550.  * its parent be the currently executing MethodContext, and sets up
  551.  * the interpreters global variables to reference the new method and
  552.  * new MethodContext.  Once those variables are set, sendMessage returns
  553.  * to the interpreter, which cheerfully begins executing the new method,
  554.  * totally unaware that the method that it was executing has changed.
  555.  *
  556.  * When a method returns, the method that called it is used to restore the
  557.  * interpreter's global variables to the state that they were in before
  558.  * the called method was called.  The values of ip and sp are restored to
  559.  * their absolute address values, and the other global state variables
  560.  * are restored accordingly.  When after the state has been restored, the
  561.  * interpreter continues execution, again totally oblivious to the fact
  562.  * that it's not running the same method it was on its previous byte code.
  563.  *
  564.  * Global state
  565.  * The following variables constitute the interpreter's state:
  566.  * ip -- the real memory address of the next byte code to be executed.
  567.  * sp -- the real memory address of the stack that's stored in the currently
  568.  *       executing block or method context.
  569.  * thisMethod -- a CompiledMethod that is the currently executing method.
  570.  * thisContextOOP -- a BlockContext or MethodContext that indicates the
  571.  *                   context that the interpreter is currently running in.
  572.  * temporaries -- physical address of the base of the method temporary
  573.  *                variables.  Typically a small number of bytes (multiple of 4
  574.  *                since it points to OOPs) lower than sp.
  575.  * self -- an OOP that is the current receiver of the current message.
  576.  * 
  577.  * Note about the interpreter:
  578.  * As an experiment, I unrolled the case statement somewhat into separate
  579.  * case arms for each byte code.  The intention was to increase performance.
  580.  * I haven't measured to see whether it makes a difference or not.
  581.  *
  582.  * The local regs concept was pre-GC.  By caching the values of IP and SP
  583.  * in local register variables, I hoped to increase performance.  I only
  584.  * needed to export the variables when I was calling out to routines that
  585.  * might change them.  However, the garbage collector may run at any time,
  586.  * and the values of IP and SP point to things in the root set and so will
  587.  * change on a GC flip.  I'm leaving the code to deal with them as local 
  588.  * registers in but conditionally compiled out until I can figure out a
  589.  * clever way to make them be registers again, or give up on the idea totally.
  590.  */
  591.  
  592. void interpret()
  593. {
  594.   Byte        ival, ival2, ival3, *savedIP;
  595.   OOP        returnedValue, *savedSP, methodContextOOP, tempOOP;
  596.   BlockContext    blockContext;
  597.   int        i;
  598.   IntState    oldSigMask;
  599. #ifdef LOCAL_REGS
  600.   register OOP    *sp;
  601.   register Byte    *ip;
  602. #endif /* LOCAL_REGS */
  603.  
  604.   importSP();
  605.   importIP();
  606.  
  607.   inInterpreter = true;
  608.  
  609.   exceptFlag = executionTracing;
  610.  
  611.   for (; ip; ) {        /* when IP is nil, return to caller */
  612.     clearGCFlipFlags();
  613.  
  614.     if (exceptFlag) {
  615.       if (abortExecution) {
  616.     goto abortMethod;    /* ugh! */
  617.       }
  618.       if (asyncQueueIndex) {    /* deal with any async signals  */
  619.     oldSigMask = disableInterrupts(); /* block out everything! */
  620.     for (i = 0; i < asyncQueueIndex; i++) {
  621.       /* ### this is not right...async signals must not allocate storage */
  622.       errorf("### Fix asyncSignal handling");
  623.       syncSignal(queuedAsyncSignals[i]);
  624.     }
  625.     asyncQueueIndex = 0;
  626.     enableInterrupts(oldSigMask);
  627.       }
  628.       if (!isNil(switchToProcess)) {
  629.     exportRegs();
  630.     changeProcessContext(switchToProcess);
  631.     importRegs();
  632.     /* make sure to validate the IP again */
  633.     continue;
  634.       }
  635.       if (executionTracing) {
  636.     printf("%5d:\t", relativeByteIndex(ip, thisMethod));
  637.     printByteCodeName(ip, relativeByteIndex(ip, thisMethod),
  638.               ((Method)oopToObj(thisMethod))->literals);
  639.     printf("\n");
  640.     if (verboseExecTracing) {
  641.       printf("\t  --> ");
  642.       printObject(stackTop());
  643.       printf("\n");
  644.     }
  645.       }
  646.       exceptFlag = executionTracing;
  647.     }
  648.       
  649.  
  650.     byteCodeCounter++;
  651. #ifdef countingByteCodes
  652.     byteCodes[*ip]++;
  653. #endif /* countingByteCodes */
  654.  
  655.     /* Note: some of the case arms are expanded out to literal cases,
  656.        instead of case0: case1: ... pushOOP(receiverVariable(self, ival&15))
  657.        this is an experiment to try to improve performance of the byte code
  658.        interpreter throughout the system. */
  659.     switch(ival = *ip++) {
  660.     case  0:    pushOOP(receiverVariable(self, 0));    break;
  661.     case  1:    pushOOP(receiverVariable(self, 1));    break;
  662.     case  2:    pushOOP(receiverVariable(self, 2));    break;
  663.     case  3:    pushOOP(receiverVariable(self, 3));    break;
  664.     case  4:    pushOOP(receiverVariable(self, 4));    break;
  665.     case  5:    pushOOP(receiverVariable(self, 5));    break;
  666.     case  6:    pushOOP(receiverVariable(self, 6));    break;
  667.     case  7:    pushOOP(receiverVariable(self, 7));    break;
  668.     case  8:    pushOOP(receiverVariable(self, 8));    break;
  669.     case  9:    pushOOP(receiverVariable(self, 9));    break;
  670.     case 10:    pushOOP(receiverVariable(self, 10));    break;
  671.     case 11:    pushOOP(receiverVariable(self, 11));    break;
  672.     case 12:    pushOOP(receiverVariable(self, 12));    break;
  673.     case 13:    pushOOP(receiverVariable(self, 13));    break;
  674.     case 14:    pushOOP(receiverVariable(self, 14));    break;
  675.     case 15:    pushOOP(receiverVariable(self, 15));    break;
  676.  
  677.     case 16:    pushOOP(methodTemporary(0));    break;
  678.     case 17:    pushOOP(methodTemporary(1));    break;
  679.     case 18:    pushOOP(methodTemporary(2));    break;
  680.     case 19:    pushOOP(methodTemporary(3));    break;
  681.     case 20:    pushOOP(methodTemporary(4));    break;
  682.     case 21:    pushOOP(methodTemporary(5));    break;
  683.     case 22:    pushOOP(methodTemporary(6));    break;
  684.     case 23:    pushOOP(methodTemporary(7));    break;
  685.     case 24:    pushOOP(methodTemporary(8));    break;
  686.     case 25:    pushOOP(methodTemporary(9));    break;
  687.     case 26:    pushOOP(methodTemporary(10));    break;
  688.     case 27:    pushOOP(methodTemporary(11));    break;
  689.     case 28:    pushOOP(methodTemporary(12));    break;
  690.     case 29:    pushOOP(methodTemporary(13));    break;
  691.     case 30:    pushOOP(methodTemporary(14));    break;
  692.     case 31:    pushOOP(methodTemporary(15));    break;
  693.  
  694.     case 32:    pushOOP(methodLiteral(thisMethod, 0));    break;
  695.     case 33:    pushOOP(methodLiteral(thisMethod, 1));    break;
  696.     case 34:    pushOOP(methodLiteral(thisMethod, 2));    break;
  697.     case 35:    pushOOP(methodLiteral(thisMethod, 3));    break;
  698.     case 36:    pushOOP(methodLiteral(thisMethod, 4));    break;
  699.     case 37:    pushOOP(methodLiteral(thisMethod, 5));    break;
  700.     case 38:    pushOOP(methodLiteral(thisMethod, 6));    break;
  701.     case 39:    pushOOP(methodLiteral(thisMethod, 7));    break;
  702.     case 40:    pushOOP(methodLiteral(thisMethod, 8));    break;
  703.     case 41:    pushOOP(methodLiteral(thisMethod, 9));    break;
  704.     case 42:    pushOOP(methodLiteral(thisMethod, 10));    break;
  705.     case 43:    pushOOP(methodLiteral(thisMethod, 11));    break;
  706.     case 44:    pushOOP(methodLiteral(thisMethod, 12));    break;
  707.     case 45:    pushOOP(methodLiteral(thisMethod, 13));    break;
  708.     case 46:    pushOOP(methodLiteral(thisMethod, 14));    break;
  709.     case 47:    pushOOP(methodLiteral(thisMethod, 15));    break;
  710.     case 48:    pushOOP(methodLiteral(thisMethod, 16));    break;
  711.     case 49:    pushOOP(methodLiteral(thisMethod, 17));    break;
  712.     case 50:    pushOOP(methodLiteral(thisMethod, 18));    break;
  713.     case 51:    pushOOP(methodLiteral(thisMethod, 19));    break;
  714.     case 52:    pushOOP(methodLiteral(thisMethod, 20));    break;
  715.     case 53:    pushOOP(methodLiteral(thisMethod, 21));    break;
  716.     case 54:    pushOOP(methodLiteral(thisMethod, 22));    break;
  717.     case 55:    pushOOP(methodLiteral(thisMethod, 23));    break;
  718.     case 56:    pushOOP(methodLiteral(thisMethod, 24));    break;
  719.     case 57:    pushOOP(methodLiteral(thisMethod, 25));    break;
  720.     case 58:    pushOOP(methodLiteral(thisMethod, 26));    break;
  721.     case 59:    pushOOP(methodLiteral(thisMethod, 27));    break;
  722.     case 60:    pushOOP(methodLiteral(thisMethod, 28));    break;
  723.     case 61:    pushOOP(methodLiteral(thisMethod, 29));    break;
  724.     case 62:    pushOOP(methodLiteral(thisMethod, 30));    break;
  725.     case 63:    pushOOP(methodLiteral(thisMethod, 31));    break;
  726.  
  727.     case 64:    pushOOP(methodVariable(thisMethod, 0));    break;
  728.     case 65:    pushOOP(methodVariable(thisMethod, 1));    break;
  729.     case 66:    pushOOP(methodVariable(thisMethod, 2));    break;
  730.     case 67:    pushOOP(methodVariable(thisMethod, 3));    break;
  731.     case 68:    pushOOP(methodVariable(thisMethod, 4));    break;
  732.     case 69:    pushOOP(methodVariable(thisMethod, 5));    break;
  733.     case 70:    pushOOP(methodVariable(thisMethod, 6));    break;
  734.     case 71:    pushOOP(methodVariable(thisMethod, 7));    break;
  735.     case 72:    pushOOP(methodVariable(thisMethod, 8));    break;
  736.     case 73:    pushOOP(methodVariable(thisMethod, 9));    break;
  737.     case 74:    pushOOP(methodVariable(thisMethod, 10));    break;
  738.     case 75:    pushOOP(methodVariable(thisMethod, 11));    break;
  739.     case 76:    pushOOP(methodVariable(thisMethod, 12));    break;
  740.     case 77:    pushOOP(methodVariable(thisMethod, 13));    break;
  741.     case 78:    pushOOP(methodVariable(thisMethod, 14));    break;
  742.     case 79:    pushOOP(methodVariable(thisMethod, 15));    break;
  743.     case 80:    pushOOP(methodVariable(thisMethod, 16));    break;
  744.     case 81:    pushOOP(methodVariable(thisMethod, 17));    break;
  745.     case 82:    pushOOP(methodVariable(thisMethod, 18));    break;
  746.     case 83:    pushOOP(methodVariable(thisMethod, 19));    break;
  747.     case 84:    pushOOP(methodVariable(thisMethod, 20));    break;
  748.     case 85:    pushOOP(methodVariable(thisMethod, 21));    break;
  749.     case 86:    pushOOP(methodVariable(thisMethod, 22));    break;
  750.     case 87:    pushOOP(methodVariable(thisMethod, 23));    break;
  751.     case 88:    pushOOP(methodVariable(thisMethod, 24));    break;
  752.     case 89:    pushOOP(methodVariable(thisMethod, 25));    break;
  753.     case 90:    pushOOP(methodVariable(thisMethod, 26));    break;
  754.     case 91:    pushOOP(methodVariable(thisMethod, 27));    break;
  755.     case 92:    pushOOP(methodVariable(thisMethod, 28));    break;
  756.     case 93:    pushOOP(methodVariable(thisMethod, 29));    break;
  757.     case 94:    pushOOP(methodVariable(thisMethod, 30));    break;
  758.     case 95:    pushOOP(methodVariable(thisMethod, 31));    break;
  759.  
  760.     case  96:    storeReceiverVariable(self, 0, popOOP());    break;
  761.     case  97:    storeReceiverVariable(self, 1, popOOP());    break;
  762.     case  98:    storeReceiverVariable(self, 2, popOOP());    break;
  763.     case  99:    storeReceiverVariable(self, 3, popOOP());    break;
  764.     case 100:    storeReceiverVariable(self, 4, popOOP());    break;
  765.     case 101:    storeReceiverVariable(self, 5, popOOP());    break;
  766.     case 102:    storeReceiverVariable(self, 6, popOOP());    break;
  767.     case 103:    storeReceiverVariable(self, 7, popOOP());    break;
  768.  
  769.     case 104:    storeMethodTemporary(0, popOOP());    break;
  770.     case 105:    storeMethodTemporary(1, popOOP());    break;
  771.     case 106:    storeMethodTemporary(2, popOOP());    break;
  772.     case 107:    storeMethodTemporary(3, popOOP());    break;
  773.     case 108:    storeMethodTemporary(4, popOOP());    break;
  774.     case 109:    storeMethodTemporary(5, popOOP());    break;
  775.     case 110:    storeMethodTemporary(6, popOOP());    break;
  776.     case 111:    storeMethodTemporary(7, popOOP());    break;
  777.  
  778.     case 112: uncheckedPushOOP(self);        break;
  779.     case 113: uncheckedPushOOP(trueOOP);    break;
  780.     case 114: uncheckedPushOOP(falseOOP);     break;
  781.     case 115: uncheckedPushOOP(nilOOP);     break;
  782.     case 116: pushInt(-1);            break;
  783.     case 117: pushInt(0);            break;
  784.     case 118: pushInt(1);            break;
  785.     case 119: pushInt(2);            break;
  786.  
  787.     case 120: case 121: case 122: case 123:
  788.       switch (ival & 3) {
  789.       case 0: uncheckedPushOOP(self);       break;
  790.       case 1: uncheckedPushOOP(trueOOP);    break;
  791.       case 2: uncheckedPushOOP(falseOOP);     break;
  792.       case 3: uncheckedPushOOP(nilOOP);     break;
  793.       }
  794.  
  795.       /* fall through */
  796.  
  797.     case 124:            /* return stack top from method */
  798. abortMethod:            /* here if ^C is seen to abort things */
  799.       returnedValue = popOOP();
  800.  
  801.       if (isBlockContext(thisContextOOP)) {
  802.     /*
  803.      * We're executing in a block context and an explicit return is
  804.      * encountered.  This means that we are to return from the caller of
  805.      * the method that created the block context, no matter how many
  806.      * levels of message sending are between where we currently are and
  807.      * our parent method context.
  808.      */
  809.     blockContext = (BlockContext)oopToObj(thisContextOOP);
  810.     methodContextOOP = blockContext->home;
  811.     if (noParentContext(methodContextOOP)) {
  812.       /* ### this should send a message to Object of some kind */
  813.       errorf("Block returning to non-existent method context");
  814.       return;
  815.     }
  816.       } else {
  817.     methodContextOOP = thisContextOOP;
  818.       }
  819.  
  820.       returnWithValue(returnedValue, methodContextOOP);
  821.       importRegs();        /* don't need to export these */
  822.       break;
  823.  
  824.     case 125:            /* return stack top from block to caller */
  825.       returnedValue = popOOP();
  826.       returnWithValue(returnedValue, thisContextOOP);
  827.       importRegs();
  828.       break;
  829.  
  830. /* 126, 127 unused by blue book, allocating 127 for debugger's
  831.    breakpoint (not yet implemented) */
  832.  
  833.     case 128:
  834.       ival2 = *ip++;
  835.       switch (ival2 >> 6) {
  836.       case 0:
  837.     pushOOP(receiverVariable(self, ival2 & 63));
  838.     break;
  839.       case 1:
  840.     pushOOP(methodTemporary(ival2 & 63));
  841.     break;
  842.       case 2:
  843.     pushOOP(methodLiteral(thisMethod, ival2 & 63));
  844.     break;
  845.       case 3:
  846.     pushOOP(methodVariable(thisMethod, ival2 & 63));
  847.     break;
  848.       }
  849.       break;
  850.  
  851.     case 129:
  852.       ival2 = *ip++;
  853.       switch (ival2 >> 6) {
  854.       case 0:
  855.     storeReceiverVariable(self, ival2 & 63, stackTop());
  856.     break;
  857.       case 1:
  858.     storeMethodTemporary(ival2 & 63, stackTop());
  859.     break;
  860.       case 2:
  861.     errorf("Attempt to store into a method constant");
  862.     break;
  863.       case 3:
  864.     storeMethodVariable(thisMethod, ival2 & 63, stackTop());
  865.       }
  866.       break;
  867.  
  868.     case 130:
  869.       ival2 = *ip++;
  870.       switch (ival2 >> 6) {
  871.       case 0:
  872.     storeReceiverVariable(self, ival2 & 63, popOOP());
  873.     break;
  874.       case 1:
  875.     storeMethodTemporary(ival2 & 63, popOOP());
  876.     break;
  877.       case 2:
  878.     errorf("Attempt to store into a method constant");
  879.     break;
  880.       case 3:
  881.     storeMethodVariable(thisMethod, ival2 & 63, popOOP());
  882.       }
  883.       break;
  884.  
  885.     case 131:            /* send selector y (xxxyyyyy), x args */
  886.       ival2 = *ip++;
  887.       /* ### Send message knows the number of arguments that are being
  888.      passed.  We could easily adjust the stack pointer here by doing
  889.      some kind of popNOOPs.  The only trouble is what happens when
  890.      the number of args doesn't agree with what the method is expecting,
  891.      and we have to generate an error.  Also, if we don't export the sp
  892.      here, we'll have to pass this as a parameter and sendMessage will
  893.      have to export it anyway.  The cost of an export or import is
  894.      about 1 or 2 instructions, so it may be cheap enough to just do
  895.      in the places that we need to to it */
  896.       exportRegs();        /* ### can this be removed? */
  897.       sendMessage(methodLiteral(thisMethod, ival2 & 31), ival2 >> 5, false);
  898.       importRegs();
  899.       break;
  900.  
  901.     case 132:            /* send selector y (xxxxxxxx,yyyyyyyy) x args*/
  902.       ival2 = *ip++;        /* the number of args */
  903.       ival3 = *ip++;        /* the selector */
  904.       exportRegs();
  905.       sendMessage(methodLiteral(thisMethod, ival3), ival2, false);
  906.       importRegs();
  907.       break;
  908.  
  909.     case 133:            /* send super selector y (xxxyyyyy), x args*/
  910.       ival2 = *ip++;
  911.       exportRegs();
  912.       sendMessage(methodLiteral(thisMethod, ival2 & 31), ival2 >> 5, true);
  913.       importRegs();
  914.       break;
  915.  
  916.     case 134:            /* send super y (xxxxxxxx,yyyyyyyy) x args */
  917.       ival2 = *ip++;        /* the number of args */
  918.       ival3 = *ip++;        /* the selector */
  919.       exportRegs();
  920.       sendMessage(methodLiteral(thisMethod, ival3), ival2, true);
  921.       importRegs();
  922.       break;
  923.  
  924.     case 135:
  925.       popOOP();
  926.       break;
  927.  
  928.     case 136:
  929.       tempOOP = stackTop();
  930.       pushOOP(tempOOP);
  931.       break;
  932.  
  933.     case 137:             /* push active context */
  934.       pushOOP(thisContextOOP);
  935.       break;
  936.  
  937.     case 144: case 145: case 146: case 147:
  938.     case 148: case 149: case 150: case 151:
  939.       ip += (ival & 7) + 1;    /* jump forward 1 to 8 bytes */
  940.       break;
  941.  
  942.     case 152: case 153: case 154: case 155:
  943.     case 156: case 157: case 158: case 159:
  944.       if (popOOP() == falseOOP) { /* jump forward if false 1 to 8 bytes */
  945.     ip += (ival & 7) + 1;
  946.       }
  947.       break;
  948.  
  949.     case 160: case 161: case 162: case 163:
  950.     case 164: case 165: case 166: case 167:
  951.       ival2 = *ip++;        /* jump forward or back */
  952.       ip += (((ival & 7) - 4) << 8) + ival2;
  953.       break;
  954.  
  955.     case 168: case 169: case 170: case 171:
  956.       ival2 = *ip++;
  957.       if (popOOP() == trueOOP) {
  958.     ip += ((ival & 3) << 8) + ival2;
  959.       }
  960.       break;
  961.  
  962.     case 172: case 173: case 174: case 175:
  963.       ival2 = *ip++;
  964.       if (popOOP() == falseOOP) {
  965.     ip += ((ival & 3) << 8) + ival2;
  966.       }
  967.       break;
  968.  
  969.     case 176: case 177: case 178: case 179:
  970.     case 180: case 181: case 182: case 183:
  971.     case 184: case 185: case 186: case 187:
  972.     case 188: case 189: case 190: case 191:
  973.                 /* send math message */
  974.       exportRegs();
  975.       sendMessage(*mathSelectors[ival & 15], 1, false);
  976.       importRegs();
  977.       break;
  978.  
  979.     case 192: case 193: case 194: case 195:
  980.     case 196: case 197: case 198: case 199:
  981.     case 200: case 201: case 202: case 203:
  982.     case 204: case 205: case 206: case 207:
  983.                 /* send special message */
  984.       exportRegs();
  985.       sendMessage(*specialMessages[ival & 15].selector,
  986.           specialMessages[ival & 15].args, false);
  987.       importRegs();
  988.       break;
  989.  
  990.     case 208: case 209: case 210: case 211:
  991.     case 212: case 213: case 214: case 215:
  992.     case 216: case 217: case 218: case 219:
  993.     case 220: case 221: case 222: case 223:
  994.                 /* send selector no args */
  995.       exportRegs();
  996.       sendMessage(methodLiteral(thisMethod, ival & 15), 0, false);
  997.       importRegs();
  998.       break;
  999.  
  1000.     case 224: case 225: case 226: case 227:
  1001.     case 228: case 229: case 230: case 231:
  1002.     case 232: case 233: case 234: case 235:
  1003.     case 236: case 237: case 238: case 239:
  1004.                 /* send selector 1 arg */
  1005.       exportRegs();
  1006.       sendMessage(methodLiteral(thisMethod, ival & 15), 1, false);
  1007.       importRegs();
  1008.       break;
  1009.  
  1010.     case 240: case 241: case 242: case 243:
  1011.     case 244: case 245: case 246: case 247:
  1012.     case 248: case 249: case 250: case 251:
  1013.     case 252: case 253: case 254: case 255:
  1014.                 /* send selector 2 args */
  1015.       exportRegs();
  1016.       sendMessage(methodLiteral(thisMethod, ival & 15), 2, false);
  1017.       importRegs();
  1018.       break;
  1019.  
  1020.     default:
  1021.       errorf("Illegal byte code %d executed\n", ival);
  1022.       break;
  1023.     }
  1024.   }
  1025.   inInterpreter = false;
  1026.  
  1027.   exportRegs();
  1028. }
  1029.  
  1030. static void changeProcessContext(newProcess)
  1031. OOP    newProcess;
  1032. {
  1033.   MethodContext thisContext, methodContext;
  1034.   OOP        processOOP, methodContextOOP;
  1035.   Process    process;
  1036.   ProcessorScheduler processor;
  1037.   
  1038.   switchToProcess = nilOOP;
  1039.   if (!isNil(thisContextOOP)) {
  1040.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  1041.     /* save old context information */
  1042.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  1043.     /* leave sp pointing to receiver, which is replaced on return with value*/
  1044.     thisContext->spOffset = fromInt(sp - thisContext->contextStack);
  1045.   }
  1046.  
  1047.   processOOP = getActiveProcess();
  1048.   process = (Process)oopToObj(processOOP);
  1049.   prepareToStore(processOOP, thisContextOOP);
  1050.   process->suspendedContext = thisContextOOP;
  1051.  
  1052.   processor = (ProcessorScheduler)oopToObj(processorOOP);
  1053.   prepareToStore(processorOOP, newProcess);
  1054.   processor->activeProcess = newProcess;
  1055.   
  1056.   process = (Process)oopToObj(newProcess);
  1057.  
  1058.   thisContextOOP = process->suspendedContext;
  1059.   /* ### should this be block context? */
  1060.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  1061.  
  1062.   methodContextOOP = getMethodContext(thisContextOOP);
  1063.  
  1064.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  1065.   thisMethod = methodContext->method;
  1066.   ip = toInt(thisContext->ipOffset) + getMethodByteCodes(thisMethod);
  1067.   sp = thisContext->contextStack + toInt(thisContext->spOffset);
  1068.  
  1069.   /* temporaries and self live in the method, not in the block */
  1070.   temporaries = methodContext->contextStack;
  1071.   self = methodContext->receiver;
  1072. }
  1073.  
  1074.  
  1075. /*
  1076.  *    static Boolean noParentContext(methodContextOOP)
  1077.  *
  1078.  * Description
  1079.  *
  1080.  *    Returns true if there is no parent context for "methodContextOOP".
  1081.  *    This occurs when the method context has been returned from, but it had
  1082.  *    created a block context during its execution and so it was not
  1083.  *    deallocated when it returned.  Now some block context is trying to
  1084.  *    return from that method context, but where to return to is undefined.
  1085.  *
  1086.  * Inputs
  1087.  *
  1088.  *    methodContextOOP: 
  1089.  *        An OOP that is the method context to be examined.
  1090.  *
  1091.  * Outputs
  1092.  *
  1093.  *    True if the current method has no parent, false otherwise.
  1094.  */
  1095. static Boolean noParentContext(methodContextOOP)
  1096. OOP methodContextOOP;
  1097. {
  1098.   MethodContext methodContext;
  1099.  
  1100.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  1101.  
  1102.   return (isNil(methodContext->sender));
  1103. }
  1104.  
  1105. /*
  1106.  *    static OOP getMethodContext(contextOOP)
  1107.  *
  1108.  * Description
  1109.  *
  1110.  *    Returns the method context for either a block context or a method
  1111.  *    context. 
  1112.  *
  1113.  * Inputs
  1114.  *
  1115.  *    contextOOP: Block or Method context OOP
  1116.  *        
  1117.  *
  1118.  * Outputs
  1119.  *
  1120.  *    Method context for CONTEXTOOP.
  1121.  */
  1122. static OOP getMethodContext(contextOOP)
  1123. OOP    contextOOP;
  1124. {
  1125.   BlockContext blockContext;
  1126.  
  1127.   if (isBlockContext(contextOOP)) {
  1128.     blockContext = (BlockContext)oopToObj(contextOOP);
  1129.     return (blockContext->home);
  1130.   } else {
  1131.     return (contextOOP);
  1132.   }
  1133. }
  1134.  
  1135. static OOP allocMethodContext()
  1136. {
  1137.   MethodContext methodContext;
  1138.  
  1139.   methodContext = (MethodContext)instantiateWith(methodContextClass,
  1140.                          CONTEXT_STACK_SIZE);
  1141.   return (allocOOP(methodContext));
  1142. }
  1143.  
  1144. static OOP allocBlockContext()
  1145. {
  1146.   BlockContext blockContext;
  1147.  
  1148.   blockContext = (BlockContext)instantiateWith(blockContextClass,
  1149.                            CONTEXT_STACK_SIZE);
  1150.   return (allocOOP(blockContext));
  1151. }
  1152.  
  1153.  
  1154. #ifdef ACCESSOR_DEBUGGING
  1155. /*
  1156.  *    static Boolean isBlockContext(contextOOP)
  1157.  *
  1158.  * Description
  1159.  *
  1160.  *    Returns true if "contextOOP" is a block context.
  1161.  *
  1162.  * Inputs
  1163.  *
  1164.  *    contextOOP: 
  1165.  *        an OOP for a context that is to be checked.
  1166.  *
  1167.  * Outputs
  1168.  *
  1169.  *    True if it's a block context, false otherwise.
  1170.  */
  1171. static Boolean isBlockContext(contextOOP)
  1172. OOP    contextOOP;
  1173. {
  1174.   return (oopClass(contextOOP) == blockContextClass);
  1175. }
  1176. #endif /* ACCESSOR_DEBUGGING */
  1177.  
  1178.  
  1179. /*
  1180.  * on entry to this routine, the stack should have the receiver and the
  1181.  * arguments pushed on the stack.  We need to get a new context,
  1182.  * setup things like the IP, SP, and Temporary pointers, and then
  1183.  * return.   Note that this routine DOES NOT invoke the interpreter; it merely
  1184.  * sets up a new context so that calling (or, more typically, returning to) the
  1185.  * interpreter will operate properly.  This kind of sending is for normal
  1186.  * messages only.  Things like sending a "value" message to a block context are
  1187.  * handled by primitives which do similar things, but they use information from
  1188.  * the block and method contexts that we don't have available (or need) here.
  1189.  */
  1190.  
  1191. void sendMessage(sendSelector, sendArgs, sendToSuper)
  1192. OOP    sendSelector;
  1193. int    sendArgs;
  1194. Boolean    sendToSuper;
  1195. {
  1196.   OOP        methodOOP, receiver, methodClass, receiverClass,
  1197.         argsArray, newContextOOP;
  1198.   MethodContext thisContext, newContext;
  1199.   MethodHeader    header;
  1200.   int        i;
  1201.   long        hashIndex;
  1202.  
  1203.   if (!sendToSuper) {
  1204.     receiver = getStackReceiver(sendArgs);
  1205.     if (isInt(receiver)) {
  1206.       receiverClass = integerClass;
  1207.     } else {
  1208.       receiverClass = oopClass(receiver);
  1209.     }
  1210.   } else {
  1211.     methodClass = getMethodClass(thisMethod);
  1212.     receiverClass = superClass(methodClass);
  1213.     receiver = self;
  1214.   }
  1215.  
  1216.   /* hash the selector and the class of the receiver together using XOR.
  1217.    * Since both are pointers to long word aligned quantities, shift over
  1218.    * by 2 bits to remove the useless low order zeros */
  1219.   hashIndex = ((long)sendSelector ^ (long)receiverClass) >> 4;
  1220.   hashIndex &= (METHOD_CACHE_SIZE - 1);
  1221.  
  1222.  
  1223.   if (methodCacheSelectors[hashIndex] == sendSelector
  1224.       && methodCacheClasses[hashIndex] == receiverClass) {
  1225.     /* :-) CACHE HIT!!! (-: */
  1226.     methodOOP = methodCacheMethods[hashIndex];
  1227.     cacheHits++;
  1228.   } else {
  1229.     /* :-( cache miss )-: */
  1230.     methodOOP = findMethod(receiverClass, sendSelector, &methodClass);
  1231.     if (isNil(methodOOP)) {
  1232.       argsArray = arrayNew(sendArgs);
  1233.       for (i = 0; i < sendArgs; i++) {
  1234.     arrayAtPut(argsArray, i+1, stackAt(sendArgs-i-1));
  1235.       }
  1236.       popNOOPs(sendArgs);
  1237.       pushOOP(messageNewArgs(sendSelector, argsArray));
  1238.       sendMessage(doesNotUnderstandColonSymbol, 1, false);
  1239.       return;
  1240.     }
  1241.     methodCacheSelectors[hashIndex] = sendSelector;
  1242.     methodCacheClasses[hashIndex] = receiverClass;
  1243.     methodCacheMethods[hashIndex] = methodOOP;
  1244.     collide[hashIndex]++;
  1245.     cacheMisses++;
  1246.   }
  1247.  
  1248.   header = getMethodHeader(methodOOP);
  1249.   if (header.numArgs != sendArgs) {
  1250.     errorf("invalid number of arguments %d, expecting %d", sendArgs,
  1251.        header.numArgs);
  1252.     return;
  1253.   }
  1254.  
  1255.   if (header.headerFlag != 0) {
  1256.     switch (header.headerFlag) {
  1257.     case 1:            /* return self */
  1258.       if (sendArgs != 0) {
  1259.     errorf("method returns primitive self, but has args!!!");
  1260.     return;
  1261.       }
  1262.  
  1263.       /* self is already on the stack...so we leave it */
  1264.       return;
  1265.  
  1266.     case 2:            /* return instance variable */
  1267.       if (sendArgs != 0) {
  1268.     errorf("method returns primitive instance variable, but has args!!!");
  1269.     return;
  1270.       }
  1271.       /* replace receiver with the returned instance variable */
  1272.       setStackTop(receiverVariable(receiver, header.numTemps));
  1273.       return;
  1274.  
  1275.     case 3:            /* send primitive */
  1276.       if (!executePrimitiveOperation(header.primitiveIndex, sendArgs,
  1277.                      methodOOP)) {
  1278.     return;
  1279.       }
  1280.       /* primitive failed.  Invoke the normal method */
  1281.       break;
  1282.     }
  1283.   }
  1284.  
  1285.   if (!isNil(thisContextOOP)) {
  1286.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  1287.     /* save old context information */
  1288.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  1289.     /* leave sp pointing to receiver, which is replaced on return with value*/
  1290.     thisContext->spOffset = fromInt(sp - sendArgs - thisContext->contextStack);
  1291.   }
  1292.  
  1293.   /* prepare the new state */
  1294.   newContextOOP = allocMethodContext();
  1295.   newContext = (MethodContext)oopToObj(newContextOOP);
  1296.   newContext->sender = thisContextOOP;
  1297.   maybeMoveOOP(methodOOP);
  1298.   newContext->method = methodOOP;
  1299.   newContext->hasBlock = nilOOP;    /* becomes non-nil when a block is created */
  1300.  
  1301.   /* copy self and sendArgs arguments into new context */
  1302.   maybeMoveOOP(sendSelector);
  1303.   newContext->selector = sendSelector;
  1304.   maybeMoveOOP(receiver);
  1305.   newContext->receiver = receiver;
  1306.   memcpy(newContext->contextStack, &sp[-sendArgs+1], (sendArgs) * sizeof(OOP));
  1307.   for (i = 0; i < sendArgs; i++) {
  1308.     maybeMoveOOP(newContext->contextStack[i]);
  1309.   }
  1310.  
  1311.   sp = &newContext->contextStack[sendArgs + header.numTemps - 1];
  1312.                 /* 1 before the actual start of stack */
  1313.  
  1314.   thisMethod = methodOOP;
  1315.   thisContextOOP = newContextOOP;
  1316.   
  1317.   temporaries = newContext->contextStack;
  1318.   self = newContext->receiver;
  1319.   ip = getMethodByteCodes(thisMethod);
  1320.   /* ### fix getmethodbytecodes to check for actual byte codes in method */
  1321. }
  1322.  
  1323.  
  1324. /*
  1325.  *    static void returnWithValue(returnedValue, returnContext)
  1326.  *
  1327.  * Description
  1328.  *
  1329.  *    Return from context "returnContext" with value "returnedValue".  Note
  1330.  *    that this context may not be the current context.  If returnContext
  1331.  *    is not a block context, then we need to carefully unwind the
  1332.  *    "method call stack".  Here carefully means that we examine each
  1333.  *    context.  If it's a block context then we cannot deallocate it.  If
  1334.  *    it's a method context, and if during its execution it did not create a
  1335.  *    block context, then we can deallocate it.  Otherwise, we need to mark
  1336.  *    it as returned (set the sender to nilOOP) and continue up the call
  1337.  *    chain until we reach returnContext.
  1338.  *
  1339.  * Inputs
  1340.  *
  1341.  *    returnedValue: 
  1342.  *        Value to be put on the stack in the sender's context.
  1343.  *    returnContext: 
  1344.  *        The context to return from, an OOP.  This may not be the
  1345.  *        current context.
  1346.  *
  1347.  */
  1348. static void returnWithValue(returnedValue, returnContext)
  1349. OOP    returnedValue, returnContext;
  1350. {
  1351.   MethodContext    oldContext, thisContext, methodContext;
  1352.   OOP            oldContextOOP, methodContextOOP;
  1353.  
  1354.   while (thisContextOOP != returnContext) {
  1355.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  1356.     if (isBlockContext(thisContextOOP)) {
  1357.       thisContextOOP = ((BlockContext)thisContext)->caller;
  1358.     } else {
  1359.       oldContextOOP = thisContextOOP;
  1360.       thisContextOOP = thisContext->sender; /* ### what if sender is nil? */
  1361.       if (!isNil(thisContext->hasBlock)) {
  1362.     /* This context created a block.  Since we don't know who is holding
  1363.        the block, we must presume that it is global.  Since any blocks
  1364.        created by this method can reference arguments and temporaries
  1365.        of this method, we must keep the method context around, but mark
  1366.        it as non-returnable so that attempts to return from it to an
  1367.        undefined place will lose. */
  1368.     thisContext->sender = nilOOP;
  1369.       }
  1370.     }
  1371.   }
  1372.  
  1373.   /* when we're here, we've deallocated any intervening contexts, and now
  1374.      we need to restore the state of the world as it was before we were called.
  1375.      Our caller has set the stack pointer to where we should place the
  1376.      return value, so all we need do is restore the interpreter's state and
  1377.      we're set. */
  1378.   /* ??? Geez, this feels clumsy.  We could have merged the "pop context"
  1379.      code below with the while loop above, using a do...while, but I wonder
  1380.      if, over the long haul, the code for popping the final context will
  1381.      be a special case and so will need separate code.  */
  1382.   oldContextOOP = thisContextOOP;
  1383.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  1384.   thisContextOOP = thisContext->sender;
  1385.  
  1386.   if (!isBlockContext(oldContextOOP)) {
  1387.     if (!isNil(thisContext->hasBlock)) {
  1388.       /* mark it so block can't return from method */
  1389.       thisContext->sender = nilOOP;
  1390.     }
  1391.   }
  1392.  
  1393.   maybeMoveOOP(thisContextOOP);
  1394.   
  1395.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  1396.  
  1397.   methodContextOOP = getMethodContext(thisContextOOP);
  1398.   if (methodContextOOP != thisContextOOP) { /* if we're a block */
  1399.     maybeMoveOOP(methodContextOOP); /* validate containing method */
  1400.   }
  1401.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  1402.   thisMethod = methodContext->method;
  1403.   maybeMoveOOP(thisMethod);
  1404.   ip = toInt(thisContext->ipOffset) + getMethodByteCodes(thisMethod);
  1405.   sp = thisContext->contextStack + toInt(thisContext->spOffset);
  1406.  
  1407.   /* temporaries and self live in the method, not in the block */
  1408.   temporaries = methodContext->contextStack;
  1409.   self = methodContext->receiver;
  1410.   maybeMoveOOP(self);
  1411.  
  1412.   maybeMoveOOP(returnedValue);
  1413.  
  1414.   setStackTop(returnedValue);
  1415. }
  1416.  
  1417.  
  1418.  
  1419. /***********************************************************************
  1420.  *
  1421.  *    Simple Method Object Accessors
  1422.  *
  1423.  ***********************************************************************/
  1424.  
  1425. #ifdef ACCESSOR_DEBUGGING
  1426.  
  1427. static OOP receiverVariable(receiver, index)
  1428. OOP    receiver;
  1429. int    index;
  1430. {
  1431.   if (!inBounds(receiver, index)) {
  1432.     errorf("Index out of bounds %d", index);
  1433.   }
  1434.   return (oopToObj(receiver)->data[index]);
  1435. }
  1436.  
  1437. static OOP getStackReceiver(numArgs)
  1438. int    numArgs;
  1439. {
  1440.   /* this is correct: numArgs == 0 means that there's just the receiver
  1441.      on the stack, at 0.  numArgs = 1 means that at location 0 is the arg,
  1442.      location 1 is the receiver. */
  1443.   return (stackAt(numArgs));
  1444. }
  1445.  
  1446. static OOP methodTemporary(index)
  1447. int    index;
  1448. {
  1449.   return (temporaries[index]);
  1450. }
  1451.  
  1452. static OOP methodLiteral(methodOOP, index)
  1453. OOP    methodOOP;
  1454. int    index;
  1455. {
  1456.   Method    method = (Method)oopToObj(methodOOP);
  1457.  
  1458.   /* ### check for in bounds with index */
  1459.   return (method->literals[index]);
  1460. }
  1461.  
  1462. static OOP methodVariable(methodOOP, index)
  1463. OOP    methodOOP;
  1464. int    index;
  1465. {
  1466.   Method    method = (Method)oopToObj(methodOOP);
  1467.  
  1468.   return (associationValue(method->literals[index]));
  1469. }
  1470.  
  1471. static Byte *getMethodByteCodes(methodOOP)
  1472. OOP    methodOOP;
  1473. {
  1474.   Method    method;
  1475.  
  1476.   if (isNil(methodOOP)) {
  1477.     return (nil);
  1478.   }
  1479.  
  1480.   method = (Method)oopToObj(methodOOP);
  1481.  
  1482.   /* skip the header and the number of literals to find the start of the
  1483.      byte codes */
  1484.   return ((Byte *)&method->literals[method->header.numLiterals]);
  1485. }
  1486.  
  1487. static MethodHeader getMethodHeader(methodOOP)
  1488. OOP    methodOOP;
  1489. {
  1490.   Method    method;
  1491.  
  1492.   method = (Method)oopToObj(methodOOP);
  1493.   return (method->header);
  1494. }
  1495.  
  1496. /*
  1497.  *    static OOP getMethodClass(method)
  1498.  *
  1499.  * Description
  1500.  *
  1501.  *    This is called when a method contains a send to "super".  The compiler
  1502.  *    is supposed to notice a send to "super", and make sure that the last
  1503.  *    literal of a method is an association between the symbol for the
  1504.  *    class of the method and the class of the method itself.  This routine
  1505.  *    returns the class of the method itself using this association.
  1506.  *
  1507.  * Inputs
  1508.  *
  1509.  *    method: An OOP that represents a method.
  1510.  *
  1511.  * Outputs
  1512.  *
  1513.  *    An OOP for the class of the method.
  1514.  */
  1515. static OOP getMethodClass(methodOOP)
  1516. OOP    methodOOP;
  1517. {
  1518.   Method    method;
  1519.   OOP        associationOOP;
  1520.  
  1521.   method = (Method)oopToObj(methodOOP);
  1522.   associationOOP = method->literals[method->header.numLiterals - 1];
  1523.   return (associationValue(associationOOP));
  1524. }
  1525.  
  1526. /***********************************************************************
  1527.  *
  1528.  *    Simple Method Object Storing routines.
  1529.  *
  1530.  ***********************************************************************/
  1531.  
  1532.  
  1533. static void storeReceiverVariable(receiver, index, oop)
  1534. OOP    receiver, oop;
  1535. int    index;
  1536. {
  1537.   if (!inBounds(receiver, index)) {
  1538.     errorf("Index out of bounds %d", index);
  1539.   }
  1540.   prepareToStore(receiver, oop);
  1541.   oopToObj(receiver)->data[index] = oop;
  1542. }
  1543.  
  1544. static void storeMethodTemporary(index, oop)
  1545. int    index;
  1546. OOP    oop;
  1547. {
  1548.   prepareToStore(thisContextOOP, oop);
  1549.   temporaries[index] = oop;
  1550. }
  1551.  
  1552. static void storeMethodVariable(methodOOP, index, oop)
  1553. OOP    methodOOP, oop;
  1554. int    index;
  1555. {
  1556.   Method    method = (Method)oopToObj(methodOOP);
  1557.  
  1558.   setAssociationValue(method->literals[index], oop);
  1559. }
  1560.  
  1561. static void storeMethodLiteral(methodOOP, index, oop)
  1562. OOP    methodOOP, oop;
  1563. int    index;
  1564. {
  1565.   Method    method = (Method)oopToObj(methodOOP);
  1566.  
  1567.   prepareToStore(methodOOP, oop);
  1568.   method->literals[index] = oop;
  1569. }
  1570.  
  1571. static Boolean inBounds(oop, index)
  1572. OOP    oop;
  1573. int    index;
  1574. {
  1575.   Object    obj = oopToObj(oop);
  1576.  
  1577.   return (index >= 0 && index < numOOPs(obj));
  1578. }
  1579. #endif /* ACCESSOR_DEBUGGING */
  1580.  
  1581. MethodHeader getMethodHeaderExt(methodOOP)
  1582. OOP    methodOOP;
  1583. {
  1584.   return (getMethodHeader(methodOOP));
  1585. }
  1586.  
  1587. void storeMethodLiteralExt(methodOOP, index, oop)
  1588. OOP    methodOOP, oop;
  1589. int    index;
  1590. {
  1591.   storeMethodLiteral(methodOOP, index, oop);
  1592. }
  1593.  
  1594. /*
  1595.  *    void storeMethodLiteralNoGC(methodOOP, index, oop)
  1596.  *
  1597.  * Description
  1598.  *
  1599.  *    This routine exists primarily for the binary save/restore code.  Rather
  1600.  *    than adding a test of the garbage collector's state to a very busy
  1601.  *    routine, it's better to create a a clone that doesn't do the prepare to
  1602.  *    store.  If this routine were more complicated, it would make sense to
  1603.  *    do the test in storeMethodLiteral (ala instVarAtPut).
  1604.  *
  1605.  * Inputs
  1606.  *
  1607.  *    methodOOP: 
  1608.  *        A method OOP to set the literal of.
  1609.  *    index : the zero-based index of the literal to set
  1610.  *    oop   : the OOP to store into the method's literal table.
  1611.  *
  1612.  */
  1613. void storeMethodLiteralNoGC(methodOOP, index, oop)
  1614. OOP    methodOOP, oop;
  1615. int    index;
  1616. {
  1617.   Method    method = (Method)oopToObj(methodOOP);
  1618.  
  1619.   method->literals[index] = oop;
  1620. }
  1621.  
  1622. /*
  1623.  *    OOP methodLiteralExt(methodOOP, index)
  1624.  *
  1625.  * Description
  1626.  *
  1627.  *    External accessor routine.  Returns a literal from the given method.
  1628.  *
  1629.  * Inputs
  1630.  *
  1631.  *    methodOOP: 
  1632.  *        A CompiledMethod OOP.
  1633.  *    index : An index into the literals of the method.
  1634.  *
  1635.  * Outputs
  1636.  *
  1637.  *    The literal at index in the CompiledMethod.
  1638.  */
  1639. OOP methodLiteralExt(methodOOP, index)
  1640. OOP    methodOOP;
  1641. int    index;
  1642. {
  1643.   return (methodLiteral(methodOOP, index));
  1644. }
  1645.  
  1646. /*
  1647.  *    Boolean equal(oop1, oop2)
  1648.  *
  1649.  * Description
  1650.  *
  1651.  *    Internal definition of equality.  Returns true if "oop1" and "oop2" are
  1652.  *    the same object, false if they are not, and false and an error if they
  1653.  *    are not the same and not both Symbols.
  1654.  *
  1655.  * Inputs
  1656.  *
  1657.  *    oop1  : An OOP to be compared, typically a Symbol.
  1658.  *    oop2  : An OOP to be compared, typically a Symbol.
  1659.  *
  1660.  * Outputs
  1661.  *
  1662.  *    True if the two objects are the same object, false if not, and an error
  1663.  *    message if they are not the same and not both symbols.
  1664.  */
  1665. Boolean equal(oop1, oop2)
  1666. OOP    oop1, oop2;
  1667. {
  1668.   if (oop1 == oop2) {
  1669.     /* no brain case (ha ha ha) */
  1670.     return (true);
  1671.   }
  1672.  
  1673.   if (isClass(oop1, symbolClass) && isClass(oop2, symbolClass)) {
  1674.     return (false);
  1675.   }
  1676.  
  1677.   errorf("Internal #= called with invalid object types\n");
  1678.   return (false);
  1679. }
  1680.  
  1681. /*
  1682.  *    long hash(oop)
  1683.  *
  1684.  * Description
  1685.  *
  1686.  *    Internal hash function.  Currently defined only for symbols, but may be
  1687.  *    extended as needed for other objects.  The definition of the hash
  1688.  *    function used here must be the same as that defined in Smalltalk
  1689.  *    methods.
  1690.  *
  1691.  * Inputs
  1692.  *
  1693.  *    oop   : An OOP to be hashed.
  1694.  *
  1695.  * Outputs
  1696.  *
  1697.  *    Hash value of the OOP, or 0 and an error message if the OOP does not
  1698.  *    have a defined has value (that this routine knows how to compute).
  1699.  */
  1700. long hash(oop)
  1701. OOP    oop;
  1702. {
  1703.   if (!isInt(oop) && oopClass(oop) == symbolClass) {
  1704.     return (oopIndex(oop));
  1705.   }
  1706.  
  1707.   errorf("Internal #hash called with invalid object type\n");
  1708.   return (0);
  1709. }
  1710.  
  1711. /*
  1712.  *    static Boolean executePrimitiveOperation(primitive, numArgs, methodOOP)
  1713.  *
  1714.  * Description
  1715.  *
  1716.  *    This routine provides the definitions of all of the primitive methods
  1717.  *    in the GNU Smalltalk system.  It normally removes the arguments to the
  1718.  *    primitive methods from the stack, but if the primitive fails, the
  1719.  *    arguments are put back onto the stack and this routine returns false,
  1720.  *    indicating failure to invoke the primitive.
  1721.  *
  1722.  * Inputs
  1723.  *
  1724.  *    primitive: 
  1725.  *        A C int that indicates the number of the primitive to invoke.
  1726.  *        Must be > 0.
  1727.  *    numArgs: 
  1728.  *        The number of arguments that the primitive has.
  1729.  *    methodOOP: 
  1730.  *        The OOP for the currently executing method.  This allows
  1731.  *        primitives to poke around in the method itself, to get at
  1732.  *        pieces that they need.  Normally, this is only used by the C
  1733.  *        callout routine to get at the compiled-in descriptor for the
  1734.  *        called C function.
  1735.  *
  1736.  * Outputs
  1737.  *
  1738.  *    True if the execution of the primitive operation succeeded, false if it
  1739.  *    failed for some reason.
  1740.  */
  1741. static Boolean executePrimitiveOperation(primitive, numArgs, methodOOP)
  1742. int    primitive, numArgs;
  1743. OOP    methodOOP;
  1744. {
  1745.   Boolean    failed, atEof;
  1746.   OOP        oop, oop1, oop2, oop3, oop4, oopVec[4], classOOP, fileOOP,
  1747.         blockContextOOP;
  1748.   long        arg1, arg2, arg3;
  1749.   double    farg1, farg2, fdummy;
  1750.   int        i, ch;
  1751.   BlockContext    blockContext;
  1752.   Byte        *fileName, *fileMode;
  1753.   FILE        *file;
  1754.   FileStream    fileStream;
  1755.   Semaphore    sem;
  1756. #if !defined(USG)
  1757.   struct timeval tv;
  1758. #else
  1759.   time_t tv;
  1760. #endif
  1761.   struct stat    statBuf;
  1762. #ifdef LOCAL_REGS
  1763.   register OOP    *sp;
  1764. #endif /* LOCAL_REGS */
  1765.  
  1766.   importSP();
  1767.  
  1768. #ifdef countingByteCodes
  1769.   primitives[primitive]++;
  1770. #endif
  1771.  
  1772.   failed = true;
  1773.   switch (primitive) {
  1774.   case  1: case  2: case  3: case  4:
  1775.   case  5: case  6: case  7: case  8:
  1776.   case  9: case 10: case 11: case 12:
  1777.   case 13: case 14: case 15: case 16:
  1778.   case 17:
  1779.     oop2 = popOOP();
  1780.     oop1 = popOOP();
  1781.     if (isInt(oop1) && isInt(oop2)) {
  1782.       failed = false;
  1783.       arg1 = toInt(oop1);
  1784.       arg2 = toInt(oop2);
  1785.       /* ??? make this faster by not pushing and popping */
  1786.  
  1787.       switch(primitive) {
  1788.       case 1:    pushInt(arg1 + arg2);        break;
  1789.       case 2:    pushInt(arg1 - arg2);        break;
  1790.       case 3:    pushBoolean(arg1 < arg2);    break;
  1791.       case 4:    pushBoolean(arg1 > arg2);    break;
  1792.       case 5:    pushBoolean(arg1 <= arg2);    break;
  1793.       case 6:    pushBoolean(arg1 >= arg2);    break;
  1794.       case 7:    pushBoolean(arg1 == arg2);    break;
  1795.       case 8:    pushBoolean(arg1 != arg2);    break;
  1796.       case 9:    pushInt(arg1 * arg2);        break; /* ### overflow? */
  1797.       case 10:
  1798.     if (arg2 != 0 /*&& (arg1 % arg2) == 0*/) { /* ### fix this when coercing goes in */
  1799.       pushInt(arg1 / arg2);
  1800.     } else {
  1801.       failed = true;
  1802.     }
  1803.     break;
  1804.       case 11:
  1805.     if (arg2 != 0) {
  1806.       if ((arg1 ^ arg2) < 0) {
  1807.         /* ??? help...is there a better way to do this? */
  1808.         pushInt(arg1 - ((arg1 - (arg2-1)) / arg2) * arg2);
  1809.       } else {
  1810.         pushInt(arg1 % arg2);
  1811.       }
  1812.     } else {
  1813.       failed = true;
  1814.     }
  1815.     break;
  1816.       case 12:
  1817.     if (arg2 != 0) {
  1818.       if ((arg1 ^ arg2) < 0) { /* differing signs => negative result */
  1819.         pushInt((arg1 - (arg2-1)) / arg2);
  1820.       } else {
  1821.         pushInt(arg1 / arg2);
  1822.       }
  1823.     } else {
  1824.       failed = true;
  1825.     }
  1826.     break;
  1827.       case 13:
  1828.     if (arg2 != 0) {
  1829.       pushInt(arg1 / arg2);
  1830.     } else {
  1831.       failed = true;
  1832.     }
  1833.     break;
  1834.       case 14:    pushInt(arg1 & arg2);          break;
  1835.       case 15:    pushInt(arg1 | arg2);        break;
  1836.       case 16:    pushInt(arg1 ^ arg2);        break;
  1837.       case 17:
  1838.     /* ??? check for overflow */
  1839.     if (arg2 >= 0) {
  1840.       pushInt(arg1 << arg2);
  1841.     } else {
  1842.       pushInt(arg1 >> -arg2);
  1843.     }
  1844.     break;
  1845.       }
  1846.     }
  1847.  
  1848.     if (failed) {
  1849.       unPop(2);
  1850.     }
  1851.     break;
  1852.  
  1853.   case 40:
  1854.     oop1 = popOOP();
  1855.     if (isInt(oop1)) {
  1856.       pushOOP(floatNew((double)toInt(oop1)));
  1857.       failed = false;
  1858.     }
  1859.  
  1860.     if (failed) {
  1861.       unPop(1);
  1862.     }
  1863.     break;
  1864.  
  1865.   case 41: case 42: case 43: case 44:
  1866.   case 45: case 46: case 47: case 48:
  1867.   case 49: case 50:
  1868.     oop2 = popOOP();
  1869.     oop1 = popOOP();
  1870.     if (isClass(oop1, floatClass) && isClass(oop2, floatClass)) {
  1871.       failed = false;
  1872.       farg1 = floatOOPValue(oop1);
  1873.       farg2 = floatOOPValue(oop2);
  1874.       switch (primitive) {
  1875.       case 41:    pushOOP(floatNew(farg1 + farg2));    break;
  1876.       case 42:    pushOOP(floatNew(farg1 - farg2));    break;
  1877.       case 43:    pushBoolean(farg1 < farg2);         break;
  1878.       case 44:    pushBoolean(farg1 > farg2);        break;
  1879.       case 45:    pushBoolean(farg1 <= farg2);        break;
  1880.       case 46:    pushBoolean(farg1 >= farg2);        break;
  1881.       case 47:    pushBoolean(farg1 == farg2);        break;
  1882.       case 48:    pushBoolean(farg1 != farg2);        break;
  1883.       case 49:    pushOOP(floatNew(farg1 * farg2));    break;
  1884.       case 50:
  1885.     if (farg2 != 0.0) {
  1886.       pushOOP(floatNew(farg1 / farg2));
  1887.     } else {
  1888.       failed = true;
  1889.     }
  1890.     break;
  1891.       }
  1892.     }
  1893.  
  1894.     if (failed) {
  1895.       unPop(2);
  1896.     }
  1897.     break;
  1898.  
  1899.   case 51:            /* Float truncated */
  1900.     oop1 = popOOP();
  1901.     if (isClass(oop1, floatClass)) {
  1902.       failed = false;
  1903.       pushInt((long)floatOOPValue(oop1));
  1904.     } else {
  1905.       unPop(1);
  1906.     }
  1907.     break;
  1908.  
  1909.   case 52:            /* Float fractionPart */
  1910.     oop1 = popOOP();
  1911.     if (isClass(oop1, floatClass)) {
  1912.       failed = false;
  1913.       farg1 = floatOOPValue(oop1);
  1914.       if (farg1 < 0.0) {
  1915.     farg1 = -farg1;
  1916.       }
  1917.       pushOOP(floatNew(modf(farg1, &fdummy)));
  1918.     } else {
  1919.       unPop(1);
  1920.     }
  1921.     break;
  1922.  
  1923.   case 53:            /* Float exponent */
  1924.     oop1 = popOOP();
  1925.     if (isClass(oop1, floatClass)) {
  1926.       failed = false;
  1927.       farg1 = floatOOPValue(oop1);
  1928.       if (farg1 == 0.0) {
  1929.     arg1 = 1;
  1930.       } else {
  1931.     frexp(floatOOPValue(oop1), (int *)&arg1);
  1932.       }
  1933.       pushInt(arg1-1);
  1934.     } else {
  1935.       unPop(1);
  1936.     }
  1937.     break;
  1938.  
  1939.   case 54:            /* Float timesTwoPower: */
  1940.     oop2 = popOOP();
  1941.     oop1 = popOOP();
  1942.     if (isClass(oop1, floatClass) && isInt(oop2)) {
  1943.       failed = false;
  1944.       farg1 = floatOOPValue(oop1);
  1945.       arg2 = toInt(oop2);
  1946. #ifdef SUNOS40
  1947.       pushOOP(floatNew(scalbn(farg1, arg2)));
  1948. #else
  1949.       pushOOP(floatNew(ldexp(farg1, arg2)));
  1950. #endif
  1951.     } else {
  1952.       unPop(2);
  1953.     }
  1954.     break;
  1955.  
  1956.   case 60:            /* Object at:, Object basicAt: */
  1957.     oop2 = popOOP();
  1958.     oop1 = popOOP();
  1959.     if (isInt(oop2)) {
  1960.       arg2 = toInt(oop2);
  1961.       if (checkIndexableBoundsOf(oop1, arg2)) {
  1962.     failed = false;
  1963.     pushOOP(indexOOP(oop1, arg2));
  1964.       }
  1965.     }
  1966.  
  1967.     if (failed) {
  1968.       unPop(2);
  1969.     }
  1970.     break;
  1971.  
  1972.   case 61:            /* Object at:put:, Object basicAt:put: */
  1973.     oop3 = popOOP();
  1974.     oop2 = popOOP();
  1975.     oop1 = popOOP();
  1976.     if (isInt(oop2)) {
  1977.       arg2 = toInt(oop2);
  1978.       if (checkIndexableBoundsOf(oop1, arg2)) {
  1979.     if (indexOOPPut(oop1, arg2, oop3)) {
  1980.       failed = false;
  1981.       pushOOP(oop3);
  1982.     }
  1983.       }
  1984.     }
  1985.  
  1986.     if (failed) {
  1987.       unPop(3);
  1988.     }
  1989.     break;
  1990.  
  1991.   case 62:            /* Object basicSize; Object size; String size;
  1992.                    ArrayedCollection size */
  1993.     oop1 = popOOP();
  1994.     pushOOP(fromInt(numIndexableFields(oop1)));
  1995.     failed = false;
  1996.     break;
  1997.  
  1998.   case 63:            /* String at:; String basicAt: */
  1999.     oop2 = popOOP();
  2000.     oop1 = popOOP();
  2001.     if (isInt(oop2)) {
  2002.       arg2 = toInt(oop2);
  2003.       if (checkIndexableBoundsOf(oop1, arg2)) {
  2004.     pushOOP(indexStringOOP(oop1, arg2));
  2005.     failed = false;
  2006.       }
  2007.     }
  2008.  
  2009.     if (failed) {
  2010.       unPop(2);
  2011.     }
  2012.     break;
  2013.  
  2014.   case 64:            /* String basicAt:put:; String at:put: */
  2015.     oop3 = popOOP();
  2016.     oop2 = popOOP();
  2017.     oop1 = popOOP();
  2018.  
  2019.     if (isInt(oop2) && isClass(oop3, charClass)) {
  2020.       arg2 = toInt(oop2);
  2021.       if (checkIndexableBoundsOf(oop1, arg2)) {
  2022.     indexStringOOPPut(oop1, arg2, oop3);
  2023.     failed = false;
  2024.     pushOOP(oop3);
  2025.       }
  2026.     }
  2027.  
  2028.     if (failed) {
  2029.       unPop(3);
  2030.     }
  2031.     break;
  2032.  
  2033.   case 68:            /* CompiledMethod objectAt: */
  2034.     oop2 = popOOP();
  2035.     oop1 = popOOP();
  2036.     if (isClass(oop1, compiledMethodClass) && isInt(oop2)) {
  2037.       arg2 = toInt(oop2);
  2038.       if (validMethodIndex(oop1, arg2)) {
  2039.     failed = false;
  2040.     pushOOP(compiledMethodAt(oop1, arg2));
  2041.       }
  2042.     }
  2043.  
  2044.     if (failed) {
  2045.       unPop(2);
  2046.     }
  2047.     break;
  2048.  
  2049.   case 69:            /* CompiledMethod objectAt:put: */
  2050.     oop3 = popOOP();
  2051.     oop2 = popOOP();
  2052.     oop1 = stackTop();
  2053.     if (isClass(oop1, compiledMethodClass) && isInt(oop2)) {
  2054.       arg2 = toInt(oop2);
  2055.       if (validMethodIndex(oop1, arg2)) {
  2056.     failed = false;
  2057.     compiledMethodAtPut(oop1, arg2, oop3);
  2058.       }
  2059.     }
  2060.  
  2061.     if (failed) {
  2062.       unPop(2);
  2063.     }
  2064.     break;
  2065.  
  2066.   case 70:            /* Behavior basicNew; Behavior new;
  2067.                    Interval class new */
  2068.     oop1 = popOOP();
  2069.     if (isOOP(oop1)) {
  2070.       if (!isIndexable(oop1)) {
  2071.     pushOOP(allocOOP(instantiate(oop1)));
  2072.     failed = false;
  2073.       }
  2074.     }
  2075.  
  2076.     if (failed) {
  2077.       unPop(1);
  2078.     }
  2079.     break;
  2080.  
  2081.  
  2082.   case 71:            /* Behavior new:; Behavior basicNew: */
  2083.     oop2 = popOOP();
  2084.     oop1 = popOOP();
  2085.     if (isOOP(oop1) && isInt(oop2)) {
  2086.       if (isIndexable(oop1)) {
  2087.     arg2 = toInt(oop2);
  2088.     pushOOP(instantiateOOPWith(oop1, arg2));
  2089.     failed = false;
  2090.       }
  2091.     }
  2092.  
  2093.     if (failed) {
  2094.       unPop(2);
  2095.     }
  2096.     break;
  2097.  
  2098.   case 72:            /* Object become: */
  2099.     oop2 = popOOP();
  2100.     oop1 = popOOP();
  2101.     if (isOOP(oop1) && isOOP(oop2)) {
  2102.       swapObjects(oop1, oop2);
  2103.       pushOOP(oop1);
  2104.       failed = false;
  2105.     }
  2106.  
  2107.     if (failed) {
  2108.       unPop(2);
  2109.     }
  2110.     break;
  2111.  
  2112.   case 73:            /* Object instVarAt: */
  2113.     oop2 = popOOP();
  2114.     oop1 = popOOP();
  2115.     if (isInt(oop2)) {
  2116.       arg2 = toInt(oop2);
  2117.       if (checkBoundsOf(oop1, arg2)) {
  2118.     failed = false;
  2119.     pushOOP(instVarAt(oop1, arg2));
  2120.       }
  2121.     }
  2122.  
  2123.     if (failed) {
  2124.       unPop(2);
  2125.     }
  2126.     break;
  2127.  
  2128.   case 74:            /* Object instVarAt:put: */
  2129.     oop3 = popOOP();
  2130.     oop2 = popOOP();
  2131.     oop1 = popOOP();
  2132.     if (isInt(oop2)) {
  2133.       arg2 = toInt(oop2);
  2134.       if (checkBoundsOf(oop1, arg2)) {
  2135.     if (instVarAtPut(oop1, arg2, oop3)) {
  2136.       failed = false;
  2137.     }
  2138.       }
  2139.     }
  2140.  
  2141.     if (failed) {
  2142.       unPop(2);
  2143.     }
  2144.     break;
  2145.  
  2146.   case 75:            /* Object asOop; Object hash; Symbol hash */
  2147.     oop1 = popOOP();
  2148.     if (isOOP(oop1)) {
  2149.       failed = false;
  2150.       pushInt(oopIndex(oop1));
  2151.     }
  2152.  
  2153.     if (failed) {
  2154.       unPop(1);
  2155.     }
  2156.     break;
  2157.  
  2158.   case 76:            /* SmallInteger asObject;
  2159.                    SmallInteger asObjectNoFail */
  2160.     oop1 = popOOP();
  2161.     if (isInt(oop1)) {
  2162.       arg1 = toInt(oop1);
  2163.       if (oopIndexValid(arg1)) {
  2164.     failed = false;
  2165.     pushOOP(oopAt(arg1-1));
  2166.       }
  2167.     }
  2168.  
  2169.     if (failed) {
  2170.       unPop(1);
  2171.     }
  2172.     break;
  2173.  
  2174.   case 77:            /* Behavior someInstance */
  2175.     oop1 = popOOP(); 
  2176.     for (oop = oopTable; oop < &oopTable[TOTAL_OOP_TABLE_SLOTS]; oop++) {
  2177.       if (oopValid(oop) && oop1 == oopClass(oop)) {
  2178.     pushOOP(oop);
  2179.     failed = false;
  2180.     break;
  2181.       }
  2182.     }
  2183.  
  2184.     if (failed) {
  2185.       unPop(1);
  2186.     }
  2187.     break;
  2188.  
  2189.   case 78:            /* Object nextInstance */
  2190.     oop1 = popOOP();
  2191.     if (!isInt(oop1)) {
  2192.       classOOP = oopClass(oop1);
  2193.       for (oop = oop1 + 1; oop < &oopTable[TOTAL_OOP_TABLE_SLOTS]; oop++) {
  2194.     if (oopValid(oop) && classOOP == oopClass(oop)) {
  2195.       failed = false;
  2196.       pushOOP(oop);
  2197.       break;
  2198.     }
  2199.       }
  2200.     }
  2201.  
  2202.     if (failed) {
  2203.       unPop(1);
  2204.     }
  2205.     break;
  2206.  
  2207.   case 79:            /* CompiledMethod class newMethod:header: */
  2208.     oop3 = popOOP();
  2209.     oop2 = popOOP();
  2210.     oop1 = popOOP();
  2211.     if (isInt(oop3) && isInt(oop2)) {
  2212.       failed = false;
  2213.       arg3 = toInt(oop3);
  2214.       arg2 = toInt(oop2);
  2215.       pushOOP(methodNewOOP(arg2, arg3));
  2216.     }
  2217.       
  2218.     if (failed) {
  2219.       unPop(3);
  2220.     }
  2221.     break;
  2222.  
  2223.   case 80:            /* ContextPart blockCopy: */
  2224.     oop2 = popOOP();
  2225.     oop1 = popOOP();
  2226.     if (isInt(oop2)) {
  2227.       failed = false;
  2228.       arg2 = toInt(oop2);
  2229.       blockContextOOP = allocBlockContext();
  2230.       blockContext = (BlockContext)oopToObj(blockContextOOP);
  2231.       blockContext->home = getMethodContext(oop1);
  2232.       maybeMoveOOP(blockContext->home);
  2233.       blockContext->numArgs = oop2;
  2234.       methodHasBlockContext(blockContext->home);
  2235.       /* the +2 here is to skip over the jump byte codes that follow the
  2236.      invocation of blockCopy, so that the ipIndex points to the first
  2237.      byte code of the block. */
  2238.       blockContext->initialIP = fromInt(relativeByteIndex(ip, thisMethod) + 2);
  2239.       if (oopClass(blockContext->home) != methodContextClass) {
  2240.     errorf("Block's home is not a MethodContext!!!\n");
  2241.       }
  2242.       pushOOP(blockContextOOP);
  2243.     }
  2244.  
  2245.     if (failed) {
  2246.       unPop(2);
  2247.     }
  2248.     break;
  2249.  
  2250.   case 81:            /* BlockContext value
  2251.                    BlockContext value:
  2252.                    BlockContext value:value:
  2253.                    BlockContext value:value:value: */
  2254.     exportSP();
  2255.     sendBlockValue(numArgs);    /* ### check number of args for agreement! */
  2256.     importSP();
  2257.  
  2258.     failed = false;
  2259.     break;
  2260.  
  2261.   case 82:            /* BlockContext valueWithArguments: */
  2262.     oop2 = popOOP();
  2263.     oop1 = stackTop();
  2264.     if (isClass(oop2, arrayClass)) {
  2265.       failed = false;
  2266.       numArgs = numIndexableFields(oop2);
  2267.       for (i = 1; i <= numArgs; i++) {
  2268.     pushOOP(arrayAt(oop2, i));
  2269.       }
  2270.       exportSP();
  2271.       sendBlockValue(numArgs);
  2272.       importSP();
  2273.     }
  2274.  
  2275.     if (failed) {
  2276.       unPop(1);
  2277.     }
  2278.     break;
  2279.  
  2280.   case 83:            /* Object perform:
  2281.                    Object perform:with:
  2282.                    Object perform:with:with:
  2283.                    Object perform:with:with:with: */
  2284.     failed = false;
  2285.     /* pop off the arguments (if any) */
  2286.     for (i = 0; i < numArgs - 1; i++) {
  2287.       oopVec[i] = popOOP();
  2288.     }
  2289.     oop1 = popOOP();        /* the selector */
  2290.     /* push the args back onto the stack */
  2291.     for (; --i >= 0; ) {
  2292.       pushOOP(oopVec[i]);
  2293.     }
  2294.     exportSP();
  2295.     sendMessage(oop1, numArgs - 1, false);
  2296.     importSP();
  2297.     break;
  2298.  
  2299.   case 84:            /* Object perform:withArguments: */
  2300.  
  2301.     oop2 = popOOP();
  2302.     oop1 = popOOP();
  2303.     if (isClass(oop2, arrayClass)) {
  2304.       failed = false;
  2305.       numArgs = numIndexableFields(oop2);
  2306.       for (i = 1; i <= numArgs; i++) {
  2307.     pushOOP(arrayAt(oop2, i));
  2308.       }
  2309.       exportSP();
  2310.       sendMessage(oop1, numArgs, false);
  2311.       importSP();
  2312.     }
  2313.  
  2314.     if (failed) {
  2315.       unPop(2);
  2316.     }
  2317.     break;
  2318.  
  2319.   case 85:            /* Semaphore signal */
  2320.     oop1 = stackTop();
  2321.     if (isClass(oop1, semaphoreClass)) {
  2322.       failed = false;
  2323.       syncSignal(oop1);
  2324.     }
  2325.  
  2326.     break;
  2327.  
  2328.   case 86:            /* Semaphore wait */
  2329.     oop1 = stackTop();
  2330.     if (isClass(oop1, semaphoreClass)) {
  2331.       failed = false;
  2332.       sem = (Semaphore)oopToObj(oop1);
  2333.       if (toInt(sem->signals) > 0) {    /* no waiting here */
  2334.     sem->signals = decrInt(sem->signals);
  2335.       } else {            /* have to suspend */
  2336.     addLastLink(oop1, getActiveProcess());
  2337.     suspendActiveProcess();
  2338.       }
  2339.     }
  2340.  
  2341.     break;
  2342.  
  2343.   case 87:            /* Process resume */
  2344.     resumeProcess(stackTop());
  2345.     failed = false;
  2346.     break;
  2347.  
  2348.   case 88:            /* Process suspend */
  2349.     oop1 = popOOP();
  2350.     if (oop1 == getActiveProcess()) {
  2351.       failed = false;
  2352.       pushOOP(nilOOP);        /* this is our return value */
  2353.       suspendActiveProcess();
  2354.     }
  2355.  
  2356.     if (failed) {
  2357.       unPop(1);
  2358.     }
  2359.     break;
  2360.  
  2361.  
  2362.   case 98:            /* Time class secondClock
  2363.                  *  -- note: this primitive has different
  2364.                  *     semantics from those defined in the
  2365.                  *     book.  This primitive returns the
  2366.                  *     seconds since Jan 1, 1970 00:00:00
  2367.                  *     instead of Jan 1,1901.
  2368.                  */
  2369.     popOOP();
  2370.     failed = false;
  2371. #if !defined(USG)
  2372.     gettimeofday(&tv, nil);
  2373.     pushInt(tv.tv_sec);
  2374. #else
  2375.     (void) time(&tv);
  2376.     pushInt(tv);
  2377. #endif
  2378.     break;
  2379.  
  2380.   case 99:            /* Time class millisecondClock
  2381.                  * -- Note: the semantics of this primitive
  2382.                  *    are different than those described in
  2383.                  *    the book.  This primitive returns the
  2384.                  *    number of milliseconds since midnight
  2385.                  *    today. */
  2386.     popOOP();
  2387.     failed = false;
  2388. #if !defined(USG)
  2389.     gettimeofday(&tv, nil);
  2390.     pushInt((tv.tv_sec % (24*60*60)) * 1000 + tv.tv_usec / 1000);
  2391. #else
  2392.     (void) time(&tv);
  2393.     pushInt((tv % (24*60*60)) * 1000);
  2394. #endif
  2395.     break;
  2396.  
  2397.   case 110:            /* Object ==, Character = */
  2398.     oop2 = popOOP();
  2399.     oop1 = popOOP();
  2400.     pushBoolean(oop1 == oop2);
  2401.     failed = false;
  2402.     break;
  2403.  
  2404.   case 111:            /* Object class */
  2405.     oop1 = popOOP();
  2406.     /* ??? is this called with ints? */
  2407.     if (isInt(oop1)) {
  2408.       pushOOP(integerClass);
  2409.     } else {
  2410.       pushOOP(oopClass(oop1));
  2411.     }
  2412.     failed = false;
  2413.     break;
  2414.  
  2415.   case 113:            /* quitPrimitive */
  2416.     exit(0);
  2417.     break;
  2418.  
  2419.   case 128:            /* Dictionary at: */
  2420.     oop2 = popOOP();
  2421.     oop1 = popOOP();
  2422.     failed = false;
  2423.     pushOOP(dictionaryAt(oop1, oop2));
  2424.     break;
  2425.  
  2426.   case 129:            /* Dictionary at: put: */
  2427.     oop3 = popOOP();
  2428.     oop2 = popOOP();
  2429.     oop1 = popOOP();
  2430.     failed = false;
  2431.     dictionaryAtPut(oop1, oop2, oop3);
  2432.     pushOOP(oop3);
  2433.     break;
  2434.  
  2435.   case 130:            /* doesNotUnderstand: message */
  2436.     oop2 = popOOP();
  2437.     oop1 = popOOP();
  2438.     printObject(oop1);
  2439.     printf(" did not understand selector '");
  2440.     printSymbol(messageSelector(oop2));
  2441.     printf("'\n\n");
  2442.     showBacktrace();
  2443.     stopExecuting(0);
  2444.     failed = false;
  2445.     break;
  2446.  
  2447.   case 131:            /* error: message */
  2448.     oop2 = popOOP();        /* error string */
  2449.     oop1 = stackTop();        /* the receiver */
  2450.     printObject(oop1);
  2451.     printf(" error: ");
  2452.     printString(oop2);
  2453.     printf("\n\n");
  2454.     showBacktrace();
  2455.     stopExecuting(0);
  2456.     failed = false;
  2457.     break;
  2458.     
  2459.   case 132:            /* Character class value: */
  2460.     oop2 = popOOP();
  2461.     oop1 = popOOP();
  2462.     if (isInt(oop2)) {
  2463.       arg2 = toInt(oop2);
  2464.       if (arg2 >= 0 && arg2 <= 255) {
  2465.     failed = false;
  2466.     pushOOP(charOOPAt(arg2));
  2467.       }
  2468.     }
  2469.  
  2470.     if (failed) {
  2471.       unPop(2);
  2472.     }
  2473.     break;
  2474.  
  2475.   case 133:            /* Character asciiValue */
  2476.     oop1 = popOOP();
  2477.     pushOOP(fromInt(charOOPValue(oop1)));
  2478.     failed = false;
  2479.     break;
  2480.  
  2481.   case 134:            /* Symbol class intern: aString */
  2482.     oop2 = popOOP();
  2483.     oop1 = popOOP();
  2484.     if (isClass(oop2, stringClass)) {
  2485.       failed = false;
  2486.       pushOOP(internStringOOP(oop2));
  2487.     }
  2488.     if (failed) {
  2489.       unPop(2);
  2490.     }
  2491.     break;
  2492.  
  2493.   case 135:            /* Dictionary new */
  2494.     popOOP();            /* ignore receiver */
  2495.     pushOOP(dictionaryNew());
  2496.     failed = false;
  2497.     break;
  2498.  
  2499.   case 136:            /* ByteMemory at: */
  2500.     oop2 = popOOP();
  2501.     oop1 = popOOP();
  2502.     if (isInt(oop2)) {
  2503.       failed = false;
  2504.       arg2 = toInt(oop2);
  2505.       pushInt(*(Byte *)arg2);
  2506.     }
  2507.  
  2508.     if (failed) {
  2509.       unPop(2);
  2510.     }
  2511.     break;
  2512.     
  2513.   case 137:            /* ByteMemory at:put: */
  2514.     oop3 = popOOP();
  2515.     oop2 = popOOP();
  2516.     if (isInt(oop2) && isInt(oop3)) {
  2517.       arg1 = toInt(oop2);
  2518.       arg2 = toInt(oop3);
  2519.       if (arg2 >= 0 && arg2 <= 255) {
  2520.     failed = false;
  2521.     *(Byte *)arg1 = arg2;
  2522.       }
  2523.     }
  2524.  
  2525.     if (failed) {
  2526.       unPop(2);
  2527.     }
  2528.     break;
  2529.     
  2530.   case 138:            /* Memory addressOfOOP: oop */
  2531.     oop2 = popOOP();
  2532.     oop1 = popOOP();
  2533.     if (!isInt(oop2)) {
  2534.       failed = false;
  2535.       pushInt((long)oop2);
  2536.     }
  2537.  
  2538.     if (failed) {
  2539.       unPop(2);
  2540.     }
  2541.     break;
  2542.  
  2543.   case 139:            /* Memory addressOf: oop */
  2544.     oop2 = popOOP();
  2545.     oop1 = popOOP();
  2546.     if (!isInt(oop2)) {
  2547.       failed = false;
  2548.       pushInt((long)oopToObj(oop2));
  2549.     }
  2550.  
  2551.     if (failed) {
  2552.       unPop(2);
  2553.     }
  2554.     break;
  2555.  
  2556.   case 140:            /* SystemDictionary backtrace */
  2557.     showBacktrace();
  2558.     failed = false;
  2559.     break;
  2560.  
  2561.  
  2562.   case 141:            /* SystemDictionary executionTrace: aBoolean */
  2563.     oop1 = popOOP();
  2564.     if (oop1 == trueOOP) {
  2565.       executionTracing = true;
  2566.     } else {
  2567.       executionTracing = false;
  2568.     }
  2569.     exceptFlag = true;
  2570.     failed = false;
  2571.     break;
  2572.  
  2573.   case 142:            /* SystemDictionary declarationTrace: aBoolean */
  2574.     oop1 = popOOP();
  2575.     if (oop1 == trueOOP) {
  2576.       declareTracing = true;
  2577.     } else {
  2578.       declareTracing = false;
  2579.     }
  2580.     failed = false;
  2581.     break;
  2582.  
  2583.   case 143:            /* ClassDescription comment: aString */
  2584.     oop2 = popOOP();
  2585.     oop1 = stackTop();
  2586.     setComment(oop1, oop2);
  2587.     failed = false;
  2588.     break;
  2589.  
  2590.   case 150:            /* methodsFor: category */
  2591.     setCompilationCategory(popOOP());
  2592.     setCompilationClass(stackTop());
  2593.     failed = false;
  2594.     break;
  2595.  
  2596.  
  2597.   case 160:            /* exp */
  2598.   case 161:            /* ln */
  2599.     oop1 = popOOP();
  2600.     if (isClass(oop1, floatClass)) {
  2601.       failed = false;
  2602.       farg1 = floatOOPValue(oop1);
  2603.       switch (primitive) {
  2604.       case 160: pushOOP(floatNew(exp(farg1)));    break;
  2605.       case 161: pushOOP(floatNew(log(farg1)));    break;
  2606.       }
  2607.     }
  2608.     if (failed) {
  2609.       unPop(1);
  2610.     }
  2611.     break;
  2612.  
  2613.   /* case 162:            /* log: aNumber -- base aNumber log */
  2614.   /* case 163:            /* floorLog: radix -- integer floor operation */
  2615.  
  2616.   case 164:            /* raisedTo: aNumber -- receiver ** aNumber */
  2617.     oop2 = popOOP();
  2618.     oop1 = popOOP();
  2619.     if (isClass(oop1, floatClass) && isClass(oop2, floatClass)) {
  2620.       failed = false;
  2621.       farg1 = floatOOPValue(oop1);
  2622.       farg2 = floatOOPValue(oop2);
  2623.       pushOOP(floatNew(pow(farg1, farg2)));
  2624.     }
  2625.     if (failed) {
  2626.       unPop(2);
  2627.     }
  2628.     break;
  2629.  
  2630.  
  2631.   /* >>>>>> HOLE 165 <<<<<< */
  2632.  
  2633.   case 166:            /* sqrt -- floating result */
  2634.     oop1 = popOOP();
  2635.     if (isClass(oop1, floatClass)) {
  2636.       failed = false;
  2637.       farg1 = floatOOPValue(oop1);
  2638.       pushOOP(floatNew(sqrt(farg1)));
  2639.     }
  2640.     if (failed) {
  2641.       unPop(1);
  2642.     }
  2643.     break;
  2644.  
  2645.   /* >>>>>> 167: HOLE <<<<<< */
  2646.  
  2647.   case 168:            /* ceiling */
  2648.   case 169:            /* floor */
  2649.     oop1 = popOOP();
  2650.     if (isClass(oop1, floatClass)) {
  2651.       failed = false;
  2652.       farg1 = floatOOPValue(oop1);
  2653.       switch (primitive) {
  2654.       case 168: pushInt((long)ceil(farg1));    break;
  2655.       case 169: pushInt((long)floor(farg1));    break;
  2656.       }
  2657.     }
  2658.     if (failed) {
  2659.       unPop(1);
  2660.     }
  2661.     break;
  2662.  
  2663.  
  2664.   case 171:            /* truncateTo: aNumber the next multiple of aNumber nearest the receiver towards zero */
  2665.   case 172:            /* rounded -- integer nearest the receiver */
  2666.   case 173:            /* roundTo: aNumber -- multiple of aNumber nearest self */
  2667.   case 174:            /* degreesToRadians */
  2668.   case 175:            /* radiansToDegrees */
  2669.  
  2670.   case 176:            /* sin */
  2671.   case 177:            /* cos */
  2672.   case 178:            /* tan */
  2673.   case 179:            /* arcSin */
  2674.   case 180:            /* arcCos */
  2675.   case 181:            /* arcTan */
  2676.     oop1 = popOOP();
  2677.     if (isClass(oop1, floatClass)) {
  2678.       failed = false;
  2679.       farg1 = floatOOPValue(oop1);
  2680.       switch (primitive) {
  2681.       case 176: pushOOP(floatNew(sin(farg1)));    break;
  2682.       case 177: pushOOP(floatNew(cos(farg1)));    break;
  2683.       case 178: pushOOP(floatNew(tan(farg1)));    break;
  2684.       case 179: pushOOP(floatNew(asin(farg1)));    break;
  2685.       case 180: pushOOP(floatNew(acos(farg1)));    break;
  2686.       case 181: pushOOP(floatNew(atan(farg1)));    break;
  2687.       }
  2688.     }
  2689.     if (failed) {
  2690.       unPop(1);
  2691.     }
  2692.     break;
  2693.  
  2694.   case 230:            /* SystemDictionary monitor: aBoolean */
  2695.     oop1 = popOOP();
  2696.     failed = false;
  2697. #ifdef USE_MONCONTROL    
  2698.     if (oop1 == trueOOP) {
  2699.       moncontrol(1);
  2700.     } else {
  2701.       moncontrol(0);
  2702.     }
  2703. #endif /* USE_MONCONTROL */
  2704.     break;
  2705.  
  2706.   case 231:            /* SystemDictionary gcMessage: aBoolean */
  2707.     oop1 = popOOP();
  2708.     failed = false;
  2709.     gcMessage = (oop1 == trueOOP);
  2710.     break;
  2711.  
  2712.   case 232:            /* SystemDictionary debug */
  2713.     failed = false;        /* used to allow dbx to stop based on st exec. */
  2714.     debug();
  2715.     break;
  2716.  
  2717.   case 233:            /* SystemDictionary verboseTrace: aBoolean */
  2718.     oop1 = popOOP();
  2719.     failed = false;
  2720.     verboseExecTracing = (oop1 == trueOOP);
  2721.     break;
  2722.     
  2723.   case 240:            /* SysFile class openFile: filename for: read-or-write */
  2724.     break;
  2725.  
  2726.   case 241:            /* SysFile close */
  2727.     break;
  2728.  
  2729.   case 242:            /* SysFile next */
  2730.     break;
  2731.  
  2732.   case 243:            /* SysFile nextPut: aCharOrByte */
  2733.     break;
  2734.  
  2735.   case 244:            /* SysFile atEnd */
  2736.     break;
  2737.  
  2738.   case 245:            /* SysFile position: anInteger */
  2739.     break;
  2740.  
  2741.   case 246:            /* SysFile size */
  2742.     break;
  2743.  
  2744.   case 247:            /* FileStream fileIn */
  2745.     oop1 = stackTop();
  2746.     fileStream = (FileStream)oopToObj(oop1);
  2747.     fileOOP = fileStream->file;
  2748.     file = (FILE *)cObjectValue(fileOOP);
  2749.     fileName = toCString(fileStream->name);
  2750.     if (access(fileName, 4) == 0) {
  2751.       failed = false;
  2752.       exportSP();
  2753.       initLexer(false);
  2754.       pushUNIXFile(file, fileName);
  2755.       yyparse();
  2756.       popStream(false);        /* we didn't open it, so we don't close it */
  2757.       importSP();
  2758.     }
  2759.     free(fileName);
  2760.     break;
  2761.  
  2762.   case 249:            /* Behavior makeDescriptorFor: funcNameString
  2763.                             returning: returnTypeSymbol
  2764.                         withArgs: argsArray */
  2765.     
  2766.     oop4 = popOOP();
  2767.     oop3 = popOOP();
  2768.     oop2 = popOOP();
  2769.     oop1 = popOOP();
  2770.     if (isClass(oop2, stringClass) && isClass(oop3, symbolClass)
  2771.     && (isClass(oop4, arrayClass)
  2772.         || isClass(oop4, undefinedObjectClass))) {
  2773.       failed = false;
  2774.       pushOOP(makeDescriptor(oop2, oop3, oop4));
  2775.     }
  2776.  
  2777.     if (failed) {
  2778.       unPop(4);
  2779.     }
  2780.     break;
  2781.  
  2782.   case 250:            /* Object snapshot */
  2783.     failed = false;
  2784.     saveToFile(defaultImageName);
  2785.     break;
  2786.  
  2787.   case 251:            /* Object snapshot: aString */
  2788.     oop2 = popOOP();
  2789.     if (isClass(oop2, stringClass)) {
  2790.       failed = false;
  2791.       fileName = toCString(oop2);
  2792.       saveToFile(fileName);
  2793.       free(fileName);
  2794.     }
  2795.  
  2796.     if (failed) {
  2797.       unPop(1);
  2798.     }
  2799.     break;
  2800.     
  2801.   case 252:            /* Object basicPrint */
  2802.     printf("Object: ");
  2803.     printObject(stackTop());
  2804.     printf("\n");
  2805.     failed = false;
  2806.     break;
  2807.  
  2808.   case 253:            /* Behavior compileString: aString */
  2809.     oop2 = popOOP();
  2810.     oop1 = stackTop();
  2811.     if (isClass(oop2, stringClass)) {
  2812.       failed = false;
  2813.       exportSP();
  2814.       initLexer(true);
  2815.       pushSmalltalkString(oop2);
  2816.       setCompilationClass(oop1);
  2817.       yyparse();
  2818.       popStream(false);        /* don't close a String! */
  2819.       importSP();
  2820.     }
  2821.     if (failed) {
  2822.       unPop(1);
  2823.     }
  2824.     break;
  2825.  
  2826.   case 254:            /* IO primitive, variadic */
  2827.     for (i = numArgs; --i >= 0; ) {
  2828.       oopVec[i] = popOOP();
  2829.     }
  2830.     oop1 = stackTop();
  2831.     if (isInt(oopVec[0])) {
  2832.       failed = false;
  2833.       arg1 = toInt(oopVec[0]);
  2834.       if (arg1 == ENUM_INT(openFilePrim) || arg1 == ENUM_INT(popenFilePrim)) {
  2835.     /* open: fileName[1] mode: mode[2] or popen: command[1] dir: direction[2] */
  2836.     fileName = toCString(oopVec[1]);
  2837.     fileMode = toCString(oopVec[2]);
  2838.     if (arg1 == ENUM_INT(openFilePrim)) {
  2839.       file = fopen((char *)fileName, (char *)fileMode);
  2840.     } else {
  2841.       file = popen((char *)fileName, (char *)fileMode);
  2842.     }
  2843.     if (file == NULL) {
  2844.       errorf("Failed to open %s named '%s'",
  2845.          (ENUM_INT(openFilePrim) == arg1) ? "file" : "pipe",
  2846.          fileName);
  2847.       free(fileName);
  2848.       free(fileMode);
  2849.       failed = true;
  2850.       break;
  2851.     }
  2852.       
  2853.     fileOOP = cObjectNew(file);
  2854.     setFileStreamFile(oop1, fileOOP, oopVec[1]);
  2855.     free(fileName);
  2856.     free(fileMode);
  2857.       } else {
  2858.     fileStream = (FileStream)oopToObj(oop1);
  2859.     fileOOP = fileStream->file;
  2860.     file = (FILE *)cObjectValue(fileOOP);
  2861.     switch (arg1) {
  2862.  
  2863.     case closeFilePrim:    /* FileStream close */
  2864.       fclose(file);
  2865.       break;
  2866.     
  2867.     case getCharPrim:    /* FileStream next */
  2868.       /* ### handle returning eof here */
  2869.       ch = getc(file);
  2870.       setStackTop(charOOPAt(ch));
  2871.       break;
  2872.       
  2873.     case putCharPrim:    /* FileStream nextPut: aChar */
  2874.       if (isClass(oopVec[1], charClass)) {
  2875.         ch = charOOPValue(oopVec[1]);
  2876.         fputc(ch, file);
  2877.       } else {
  2878.         failed = true;
  2879.       }
  2880.       break;
  2881.  
  2882.     case seekPrim:        /* FileStream position: position */
  2883.       fseek(file, toInt(oopVec[1]), 0);
  2884.       break;
  2885.  
  2886.     case tellPrim:        /* FileStream position */
  2887.       setStackTop(fromInt(ftell(file)));
  2888.       break;
  2889.  
  2890.     case eofPrim:        /* FileStream atEnd */ 
  2891.       popOOP();        /* remove self */
  2892.       ch = fgetc(file);
  2893.       atEof = feof(file);
  2894.       pushBoolean(atEof);
  2895.       ungetc(ch, file);
  2896.       break;
  2897.  
  2898.     case sizePrim:
  2899.       if (fstat(fileno(file), &statBuf)) {
  2900.         failed = true;
  2901.       } else {
  2902.         setStackTop(fromInt(statBuf.st_size));
  2903.       }
  2904.       break;
  2905.     }
  2906.       }
  2907.     }
  2908.  
  2909.     if (failed) {
  2910.       unPop(numArgs);
  2911.     }
  2912.     break;
  2913.  
  2914.   case 255:            /* C callout primitive */
  2915.     inInterpreter = false;
  2916.     exportSP();
  2917.     inCCode = true;
  2918.     if (setjmp(cCalloutJmpBuf) == 0) {
  2919.       invokeCRoutine(numArgs, methodOOP);
  2920.     }
  2921.     inCCode = false;
  2922.     importSP();
  2923.     inInterpreter = true;
  2924.     failed = false;
  2925.     break;
  2926.  
  2927.   default:
  2928.     errorf("Unhandled primitive operation %d", primitive);
  2929.   }
  2930.  
  2931.   exportSP();
  2932.   return (failed);
  2933.  
  2934. }
  2935.  
  2936. /*
  2937. These are the primitives as defined in the Blue Book.  The ones with numbers
  2938. but without stars are those which are not implemented.
  2939.  
  2940. * 1 +
  2941. * 2 -
  2942. * 3 <
  2943. * 4 >
  2944. * 5 <=
  2945. * 6 >=
  2946. * 7 =
  2947. * 8 ~=
  2948. * 9 *
  2949. * 10 /
  2950. * 11 \\
  2951. * 12 //
  2952. * 13 quo:
  2953. * 14 bitAnd:
  2954. * 15 bitOr:
  2955. * 16 bitXor:
  2956. * 17 bitShift:
  2957.  
  2958. * 40 Smallinteger asFloat
  2959. * 41 Float +
  2960. * 42 Float -
  2961. * 43 Float >
  2962. * 44 Float <
  2963. * 45 Float <=
  2964. * 46 Float >=
  2965. * 47 Float =
  2966. * 48 Float ~=
  2967. * 49 Float *
  2968. * 50 Float /
  2969. * 51 Float truncated
  2970. * 52 Float fractionPart
  2971. * 53 Float exponent
  2972. * 54 Float timesTwoPower:
  2973.  
  2974. * 60 Object at:
  2975.    Object basicAt:
  2976. * 61 Object basicAt:put:
  2977.    Object at:put:
  2978. * 62 Object basicSize
  2979.    Object size
  2980.    String size
  2981.    ArrayedCollection size
  2982. * 63 String at:
  2983.    String basicAt:
  2984. * 64 String basicAt:put:
  2985.    String at:put:
  2986.  
  2987. * 70 Behavior basicNew
  2988.    Behavior new
  2989.    Interval class new
  2990. * 71 Behavior new:
  2991.    Behavior basicNew:
  2992. * 72 Object become:
  2993. * 73 Object instVarAt:
  2994. * 74 Object instVarAt:put:
  2995. * 75 Object asOop
  2996.    Object hash
  2997.    Symbol hash
  2998. * 76 SmallInteger asObject
  2999.    SmallInteger asObjectNoFail
  3000. * 77 Behavior someInstance
  3001. * 78 Object nextInstance
  3002. 79 CompiledMethod class newMethod:header:
  3003. * 80 ContextPart blockCopy:
  3004. * 81 BlockContext value:value:value:
  3005.    BlockContext value:
  3006.    BlockContext value:value:
  3007. * 82 BlockContext valueWithArguments:
  3008. * 83 Object perform:with:with:with:
  3009.    Object perform:with:
  3010.    Object perform:with:with:
  3011.    Object perform:
  3012. * 84 Object perform:withArguments:
  3013.  
  3014. 105 ByteArray primReplaceFrom:to:with:startingAt:
  3015.     ByteArray replaceFrom:to:withString:startingAt:
  3016.     String replaceFrom:to:withByteArray:startingAt:
  3017.     String primReplaceFrom:to:with:startingAt:
  3018.  
  3019. * 110 Character =
  3020.     Object ==
  3021. * 111 Object class
  3022.  
  3023. */
  3024.  
  3025. static OOP getActiveProcess()
  3026. {
  3027.   ProcessorScheduler processor;
  3028.  
  3029.   if (!isNil(switchToProcess)) {
  3030.     return (switchToProcess);
  3031.   } else {
  3032.     processor = (ProcessorScheduler)oopToObj(processorOOP);
  3033.     return (processor->activeProcess);
  3034.   }
  3035. }
  3036.  
  3037. static void addLastLink(semaphoreOOP, processOOP)
  3038. OOP    semaphoreOOP, processOOP;
  3039. {
  3040.   Semaphore    sem;
  3041.   Process    process, lastProcess;
  3042.   OOP        lastProcessOOP;
  3043.  
  3044.   prepareToStore(processOOP, semaphoreOOP);
  3045.   process = (Process)oopToObj(processOOP);
  3046.   process->myList = semaphoreOOP;
  3047.   process->nextLink = nilOOP;
  3048.  
  3049.   sem = (Semaphore)oopToObj(semaphoreOOP);
  3050.   if (isNil(sem->lastLink)) {
  3051.     prepareToStore(semaphoreOOP, processOOP);
  3052.     sem = (Semaphore)oopToObj(semaphoreOOP);
  3053.     sem->firstLink = sem->lastLink = processOOP;
  3054.   } else {
  3055.     lastProcessOOP = sem->lastLink;
  3056.     prepareToStore(lastProcessOOP, processOOP);
  3057.     lastProcess = (Process)oopToObj(lastProcessOOP);
  3058.     lastProcess->nextLink = processOOP;
  3059.     prepareToStore(semaphoreOOP, processOOP);
  3060.     sem = (Semaphore)oopToObj(semaphoreOOP);
  3061.     sem->lastLink = processOOP;
  3062.   }
  3063. }
  3064.  
  3065. syncSignal(semaphoreOOP)
  3066. OOP    semaphoreOOP;
  3067. {
  3068.   Semaphore sem;
  3069.  
  3070.   sem = (Semaphore)oopToObj(semaphoreOOP);
  3071.   if (isEmpty(semaphoreOOP)) {    /* nobody waiting */
  3072.     sem->signals = incrInt(sem->signals);
  3073.   } else {
  3074.     resumeProcess(removeFirstLink(semaphoreOOP));
  3075.   }
  3076. }
  3077.  
  3078. static OOP removeFirstLink(semaphoreOOP)
  3079. OOP    semaphoreOOP;
  3080. {
  3081.   Semaphore    sem;
  3082.   Process    process;
  3083.   OOP        processOOP;
  3084.  
  3085.   sem = (Semaphore)oopToObj(semaphoreOOP);
  3086.   processOOP = sem->firstLink;
  3087.   process = (Process)oopToObj(processOOP);
  3088.   prepareToStore(semaphoreOOP, process->nextLink);
  3089.   sem->firstLink = process->nextLink;
  3090.   if (isNil(sem->firstLink)) {
  3091.     sem->lastLink = nilOOP;
  3092.   }
  3093.  
  3094.   process->nextLink = nilOOP;
  3095.   process->myList = nilOOP;
  3096.  
  3097.   return (processOOP);
  3098. }
  3099.  
  3100. static void resumeProcess(processOOP)
  3101. OOP    processOOP;
  3102. {
  3103.   OOP        activeOOP;
  3104.   Process    process, active;
  3105.  
  3106.   activeOOP = getActiveProcess();
  3107.   active = (Process)oopToObj(activeOOP);
  3108.   process = (Process)oopToObj(processOOP);
  3109.  
  3110.   if (toInt(process->priority) > toInt(active->priority)) {
  3111.     /*
  3112.      * we're resuming a process with a higher priority, so sleep the
  3113.      * current one and activate the new one
  3114.      */
  3115.     sleepProcess(activeOOP);
  3116.     activateProcess(processOOP);
  3117.   } else {
  3118.     /* this process isn't higher than the active one */
  3119.     sleepProcess(processOOP);
  3120.   }
  3121. }
  3122.  
  3123. static void activateProcess(processOOP)
  3124. OOP    processOOP;
  3125. {
  3126.   switchToProcess = processOOP;
  3127.   exceptFlag = true;
  3128. }
  3129.  
  3130. static void sleepProcess(processOOP)
  3131. OOP    processOOP;
  3132. {
  3133.   Process    process;
  3134.   int        priority;
  3135.   OOP        processLists;
  3136.   OOP        processList;
  3137.  
  3138.   process = (Process)oopToObj(processOOP);
  3139.   priority = toInt(process->priority);
  3140.   processLists = getProcessLists();
  3141.   processList = arrayAt(processLists, priority);
  3142.  
  3143.   /* add process to end of priority queue */
  3144.   addLastLink(processList, processOOP);
  3145. }
  3146.  
  3147.  
  3148. static void suspendActiveProcess()
  3149. {
  3150.   activateProcess(highestPriorityProcess());
  3151. }
  3152.  
  3153.  
  3154. static OOP highestPriorityProcess()
  3155. {
  3156.   OOP        processLists, processList;
  3157.   int        priority;
  3158.  
  3159.   processLists = getProcessLists();
  3160.   priority = numOOPs(oopToObj(processLists));
  3161.   for (; priority > 0 ; priority--) {
  3162.     processList = arrayAt(processLists, priority);
  3163.     if (!isEmpty(processList)) {
  3164.       return (removeFirstLink(processList));
  3165.     }
  3166.   }
  3167.  
  3168.   errorf("No Runnable process!!!");
  3169.   exit(0);
  3170. }
  3171.  
  3172. static Boolean isEmpty(processListOOP)
  3173. OOP    processListOOP;
  3174. {
  3175.   Semaphore    processList;
  3176.  
  3177.   processList = (Semaphore)oopToObj(processListOOP);
  3178.   return (isNil(processList->firstLink));
  3179. }
  3180.  
  3181. static OOP getProcessLists()
  3182. {
  3183.   ProcessorScheduler processor;
  3184.  
  3185.   processor = (ProcessorScheduler)oopToObj(processorOOP);
  3186.   return (processor->processLists);
  3187. }
  3188.  
  3189. static OOP semaphoreNew()
  3190. {
  3191.   Semaphore    sem;
  3192.  
  3193.   sem = (Semaphore)instantiate(semaphoreClass);
  3194.   sem->signals = fromInt(0);
  3195.  
  3196.   return (allocOOP(sem));
  3197. }
  3198.  
  3199.  
  3200. /*
  3201.  *    static void methodHasBlockContext(methodOOP)
  3202.  *
  3203.  * Description
  3204.  *
  3205.  *    Marks a method context has having created a block context.
  3206.  *
  3207.  * Inputs
  3208.  *
  3209.  *    methodOOP: MethodContext OOP to be marked
  3210.  *        
  3211.  *
  3212.  */
  3213. static void methodHasBlockContext(methodOOP)
  3214. OOP    methodOOP;
  3215. {
  3216.   MethodContext methodContext;
  3217.  
  3218.   methodContext = (MethodContext)oopToObj(methodOOP);
  3219.   /* Since trueOOP is in the root set, we don't have to prepare to store it */
  3220.   methodContext->hasBlock = trueOOP;
  3221. }
  3222.  
  3223. void setFileStreamFile(fileStreamOOP, fileOOP, fileNameOOP)
  3224. OOP    fileStreamOOP, fileOOP, fileNameOOP;
  3225. {
  3226.   FileStream    fileStream;
  3227.  
  3228.   fileStream = (FileStream)oopToObj(fileStreamOOP);
  3229.   prepareToStore(fileStreamOOP, fileOOP);
  3230.   fileStream->file = fileOOP;
  3231.   prepareToStore(fileStreamOOP, fileNameOOP);
  3232.   fileStream->name = fileNameOOP;
  3233. }
  3234.  
  3235.  
  3236. /*
  3237.  *    static void sendBlockValue(numArgs)
  3238.  *
  3239.  * Description
  3240.  *
  3241.  *    This is the equivalent of sendMessage, but is for blocks.  The block
  3242.  *    context that is to the the receiver of the "value" message should be
  3243.  *    "numArgs" into the stack.  Temporaries come from the block's method
  3244.  *    context, as does self.  IP is set to the proper
  3245.  *    place within the block's method's byte codes, and SP is set to the top
  3246.  *    of the arguments in the block context, which have been copied out of
  3247.  *    the caller's context.
  3248.  *
  3249.  * Inputs
  3250.  *
  3251.  *    numArgs: 
  3252.  *        The number of arguments sent to the block.
  3253.  *
  3254.  */
  3255. static void sendBlockValue(numArgs)
  3256. int    numArgs;
  3257. {
  3258.   OOP        selector, blockContextOOP, methodContextOOP;
  3259.   BlockContext    blockContext;
  3260.   MethodContext thisContext, methodContext;
  3261.   int        i;
  3262.  
  3263.   if (!isNil(thisContextOOP)) {
  3264.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  3265.     /* save old context information */
  3266.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  3267.     /* leave sp pointing to receiver, which is replaced on return with value*/
  3268.     thisContext->spOffset = fromInt(sp - numArgs - thisContext->contextStack);
  3269.   }
  3270.  
  3271.   /* prepare the new state */
  3272.   blockContextOOP = stackAt(numArgs);
  3273.   maybeMoveOOP(blockContextOOP); /* make sure we're alive */
  3274.  
  3275.   blockContext = (BlockContext)oopToObj(blockContextOOP);
  3276.   maybeMoveOOP(thisContextOOP);    /* ### not sure if this is needed*/
  3277.   blockContext->caller = thisContextOOP;
  3278.   switch (numArgs) {
  3279.   case 0:    blockContext->selector = valueSymbol; break;
  3280.   case 1:    blockContext->selector = valueColonSymbol; break;
  3281.   case 2:    blockContext->selector = valueColonValueColonSymbol; break;
  3282.   case 3:    blockContext->selector = valueColonValueColonValueColonSymbol; break;
  3283.   default:    blockContext->selector = valueWithArgumentsColonSymbol; break;
  3284.   }
  3285.  
  3286.   /* home is set when the block is created */
  3287.  
  3288.   /* copy numArgs arguments into new context */
  3289.   memcpy(blockContext->contextStack, &sp[-numArgs+1],
  3290.      (numArgs) * sizeof(OOP));
  3291.   for (i = 0; i < numArgs; i++) {
  3292.     maybeMoveOOP(blockContext->contextStack[i]);
  3293.   }
  3294.  
  3295.   sp = &blockContext->contextStack[numArgs-1]; /* start of stack-1 */
  3296.  
  3297.   thisContextOOP = blockContextOOP;
  3298.  
  3299.   methodContextOOP = blockContext->home;
  3300.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  3301.   ip = toInt(blockContext->initialIP) + getMethodByteCodes(methodContext->method);
  3302.   thisMethod = methodContext->method;
  3303.   maybeMoveOOP(thisMethod);
  3304.   temporaries = methodContext->contextStack;
  3305.   self = methodContext->receiver;
  3306.   maybeMoveOOP(self);
  3307. }
  3308.  
  3309. /*
  3310.  *    static char *selectorAsString(selector)
  3311.  *
  3312.  * Description
  3313.  *
  3314.  *    Converts a selector to a C string object
  3315.  *
  3316.  * Inputs
  3317.  *
  3318.  *    selector: A OOP for the selector, a Symbol.
  3319.  *        
  3320.  *
  3321.  * Outputs
  3322.  *
  3323.  *    C string that corresponds to the selector's printed name.
  3324.  */
  3325. static char *selectorAsString(selector)
  3326. OOP    selector;
  3327. {
  3328.   return (symbolAsString(selector));
  3329. }
  3330.  
  3331. /*
  3332.  *    static OOP findMethod(receiverClass, selector)
  3333.  *
  3334.  * Description
  3335.  *
  3336.  *    Scans the methods of "receiverClass" and all its super classes for one
  3337.  *    with selector "selector".  It returns the method if it found, otherwise
  3338.  *    nil is returned.
  3339.  *
  3340.  * Inputs
  3341.  *
  3342.  *    receiverClass:
  3343.  *        The class to begin the search in.  This is normally called from
  3344.  *        the message sending code, so that's why this parameter is
  3345.  *        called receiverClass.
  3346.  *    selector:
  3347.  *        The selector for the method that is being sought.
  3348.  *    methodClassPtr:
  3349.  *        The class that the method was eventually found in.  Passed
  3350.  *        by reference and set when returning.
  3351.  *
  3352.  * Outputs
  3353.  *
  3354.  *    Method for "selector", or nilOOP if not found.  "methodClassPtr" is
  3355.  *    returned as a by-reference parameter.
  3356.  */
  3357. static OOP findMethod(receiverClass, selector, methodClassPtr)
  3358. OOP    receiverClass, selector, *methodClassPtr;
  3359. {
  3360.   OOP        classOOP, methodOOP;
  3361.  
  3362.   for (classOOP = receiverClass; !isNil(classOOP);
  3363.        classOOP = superClass(classOOP)) {
  3364.     methodOOP = findClassMethod(classOOP, selector);
  3365.     if (!isNil(methodOOP)) {
  3366.       *methodClassPtr = classOOP;
  3367.       return (methodOOP);
  3368.     }
  3369.   }
  3370.  
  3371.   *methodClassPtr = undefinedObjectClass; /* probably not used */
  3372.   return (nilOOP);
  3373. }
  3374.  
  3375. /* runs before GC turned on */
  3376. void initProcessSystem()
  3377. {
  3378.   OOP        processLists;
  3379.   int        i;
  3380.   ProcessorScheduler processor;
  3381.   Process    initialProcess;
  3382.   OOP        initialProcessOOP;
  3383.   
  3384.  
  3385.   processLists = arrayNew(NUM_PRIORITIES);
  3386.  
  3387.   for (i = 1; i <= NUM_PRIORITIES; i++) {
  3388.     arrayAtPut(processLists, i, semaphoreNew()); /* ### should be linked list */
  3389.   }
  3390.  
  3391.   initialProcess = (Process)instantiate(processClass);
  3392.   initialProcess->priority = fromInt(4);
  3393.   initialProcessOOP = allocOOP(initialProcess);
  3394.  
  3395.  
  3396.   processor = (ProcessorScheduler)instantiate(processorSchedulerClass);
  3397.   processor->processLists = processLists;
  3398.   processor->activeProcess = initialProcessOOP;
  3399.  
  3400.   processorOOP = allocOOP(processor);
  3401. }
  3402.  
  3403.  
  3404. void initInterpreter()
  3405. {
  3406.   thisContextOOP = nilOOP;
  3407.   asyncQueueIndex = 0;
  3408.   switchToProcess = nilOOP;
  3409. }
  3410.  
  3411. void prepareExecutionEnvironment()
  3412. {
  3413.   MethodContext thisContext, newContext;
  3414.   OOP        newContextOOP;
  3415.  
  3416.   abortExecution = false;
  3417.  
  3418.   if (!isNil(thisContextOOP)) {
  3419.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  3420.     /* save old context information */
  3421.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  3422.     /* leave sp pointing to receiver, which is replaced on return with value*/
  3423.     thisContext->spOffset = fromInt(sp - thisContext->contextStack);
  3424.   }
  3425.  
  3426.   /* now make a dummy context to run with */
  3427.   newContextOOP = allocMethodContext();
  3428.   newContext = (MethodContext)oopToObj(newContextOOP);
  3429.   ip = nil;
  3430.   maybeMoveOOP(thisContextOOP);
  3431.   newContext->sender = thisContextOOP;
  3432.   thisMethod = newContext->method = nilOOP;
  3433.   newContext->selector = nilOOP; /* no real selector invoked us */
  3434.   newContext->receiver = nilOOP; /* make self be real (well, nil) */
  3435.   sp = newContext->contextStack - 1;
  3436.  
  3437.   temporaries = newContext->contextStack;
  3438.   self = nilOOP;
  3439.  
  3440.   thisContextOOP = newContextOOP;
  3441.  
  3442.   invalidateMethodCache();
  3443. #ifdef countingByteCodes 
  3444.   initByteCodeCounter();
  3445. #endif
  3446. }
  3447.  
  3448. OOP finishExecutionEnvironment()
  3449. {
  3450.   MethodContext oldContext, thisContext;
  3451.   OOP        oldContextOOP, returnedValue;
  3452.   
  3453.   returnedValue = stackTop();
  3454.   oldContextOOP = thisContextOOP;
  3455.   oldContext = (MethodContext)oopToObj(oldContextOOP);
  3456.   thisContextOOP = oldContext->sender;
  3457.  
  3458.   if (!isNil(thisContextOOP)) {
  3459.     maybeMoveOOP(thisContextOOP);
  3460.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  3461.     /* restore old context information */
  3462.     thisMethod = thisContext->method;
  3463.     maybeMoveOOP(thisMethod);
  3464.     temporaries = thisContext->contextStack;
  3465.     self = thisContext->receiver;
  3466.     maybeMoveOOP(self);
  3467.     ip = toInt(thisContext->ipOffset) + getMethodByteCodes(thisMethod);
  3468.     sp = thisContext->contextStack + toInt(thisContext->spOffset);
  3469.   }
  3470.   return (returnedValue);
  3471. }
  3472.  
  3473. static void invalidateMethodCache()
  3474. {
  3475.   register int    i;
  3476.  
  3477.   cacheHits = cacheMisses = 0;
  3478.  
  3479.   for (i = 0; i < METHOD_CACHE_SIZE; i++) {
  3480.     methodCacheSelectors[i] = primitiveCacheSelectors[i] = nilOOP;
  3481.     methodCacheClasses[i] = primitiveCacheClasses[i] = nilOOP;
  3482.     methodCacheMethods[i] = nilOOP;
  3483.     collide[0] = primitiveCachePrimitives[i] = 0;
  3484.   }
  3485. }
  3486.  
  3487. void updateMethodCache(selectorOOP, classOOP, methodOOP)
  3488. OOP    selectorOOP, classOOP, methodOOP;
  3489. {
  3490.   long        hashIndex;
  3491.  
  3492.   hashIndex = ((long)selectorOOP ^ (long)classOOP) >> 4;
  3493.   hashIndex &= (METHOD_CACHE_SIZE - 1);
  3494.   if (methodCacheSelectors[hashIndex] == selectorOOP &&
  3495.       methodCacheClasses[hashIndex] == classOOP) {
  3496.     methodCacheMethods[hashIndex] = methodOOP;
  3497.   }
  3498. }
  3499.  
  3500. #ifdef countingByteCodes
  3501. initByteCodeCounter()
  3502. {
  3503.   int i;
  3504.  
  3505.   for (i = 0; i < 256; i++) {
  3506.     primitives[i] = byteCodes[i] = 0;
  3507.   }
  3508. }
  3509.  
  3510. printByteCodeCounts()
  3511. {
  3512.   int i;
  3513.  
  3514.   for (i = 0; i < 256; i++) {
  3515.     if (byteCodes[i]) {
  3516.       printf("Byte code %d = %d\n", i, byteCodes[i]);
  3517.     }
  3518.   }
  3519.  
  3520.   printf("\n---> primitives:\n");
  3521.   for (i = 0; i < 256; i++) {
  3522.     if (primitives[i]) {
  3523.       printf("Primitive %d = %d\n", i, primitives[i]);
  3524.     }
  3525.   }
  3526.  
  3527. }
  3528. #endif
  3529.  
  3530. relativeByteIndex(bp, methodOOP)
  3531. Byte    *bp;
  3532. OOP    methodOOP;
  3533. {
  3534.   return (bp - getMethodByteCodes(methodOOP));
  3535. }
  3536.  
  3537. /*
  3538.  *    void moveProcessorRegisters()
  3539.  *
  3540.  * Description
  3541.  *
  3542.  *    Part of the GC flip, copy the root set process.  This ensures that the
  3543.  *    processor registers are pointing to objects in new space.  The term
  3544.  *    "processor registers" refers here to interpreter variables like ip, sp,
  3545.  *    temporaries, etc.
  3546.  *
  3547.  */
  3548. void moveProcessorRegisters()
  3549. {
  3550.   MethodContext thisContext;    /* may be block context, but doesn't matter */
  3551.   MethodContext    methodContext;
  3552.   int        spOffset, ipOffset;
  3553.   OOP        methodContextOOP;
  3554.  
  3555.   invalidateMethodCache();
  3556.  
  3557.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  3558.   spOffset = sp - thisContext->contextStack;
  3559.   ipOffset = relativeByteIndex(ip, thisMethod);
  3560.  
  3561.   maybeMoveOOP(thisContextOOP);
  3562.   maybeMoveOOP(thisMethod);
  3563.  
  3564.   ip = ipOffset + getMethodByteCodes(thisMethod);
  3565.  
  3566.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  3567.   sp = thisContext->contextStack + spOffset;
  3568.  
  3569.   methodContextOOP = getMethodContext(thisContextOOP);
  3570.   maybeMoveOOP(methodContextOOP);
  3571.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  3572.  
  3573.   temporaries = methodContext->contextStack;
  3574. /*** just as a test...I don't remember why the comment says self remains valid,
  3575.  *** because in my tests, it's pointing to old-space data...for long running
  3576.  *** executions, this could be disasterous 
  3577.  */
  3578.   /* self remains valid */
  3579. maybeMoveOOP(self);
  3580.  
  3581.   moveSemaphoreOOPs();
  3582. }
  3583.  
  3584. /*
  3585.  *    static void moveSemaphoreOOPs()
  3586.  *
  3587.  * Description
  3588.  *
  3589.  *    This routine doesn't really do anything yet.  It's intended purpose is
  3590.  *    to be called during the root set copying part of a GC flip to copy any
  3591.  *    asynchronous semaphores.  However, the async semaphore representation
  3592.  *    is likely not to be in terms of Smalltalk objects for a variety of
  3593.  *    reasons, so the need for this routine may never materialize.
  3594.  *
  3595.  */
  3596. static void moveSemaphoreOOPs()
  3597. {
  3598.   int        i;
  3599.   IntState    oldSigMask;
  3600.  
  3601.   oldSigMask = disableInterrupts(); /* block out everything! */
  3602.   /* ### this needs to be changed; async signals shouldn't be oops! */
  3603.   for (i = 0; i < asyncQueueIndex; i++) {
  3604.     moveOOP(queuedAsyncSignals[i]);
  3605.   }
  3606.   enableInterrupts(oldSigMask);
  3607. }
  3608.  
  3609. /*
  3610.  *    void initSignals()
  3611.  *
  3612.  * Description
  3613.  *
  3614.  *    Trap the signals that we care about, basically SIGBUG and SIGSEGV.
  3615.  *    These are sent to the back trace routine so we can at least have some
  3616.  *    idea of where we were when we died.
  3617.  *
  3618.  */
  3619. void initSignals()
  3620. {
  3621.   signal(SIGBUS, interruptHandler);
  3622.   signal(SIGSEGV, interruptHandler);
  3623.  
  3624.   signal(SIGINT, stopExecuting);
  3625. }
  3626.  
  3627.  
  3628. /*
  3629.  *    static signalType stopExecuting(sig)
  3630.  *
  3631.  * Description
  3632.  *
  3633.  *    Sets flags so that the interpreter starts returning immediately
  3634.  *    from whatever byte codes it's executing.  It returns via normal method
  3635.  *    returns, so that the world is in a consistent state when it's done.
  3636.  *
  3637.  * Inputs
  3638.  *
  3639.  *    sig   : signal that caused the interrupt (typically ^C), or 0, which
  3640.  *        comes from a call from within the system.
  3641.  *
  3642.  */
  3643. static signalType stopExecuting(sig)
  3644. {
  3645.   if (sig) {
  3646.     printf("\nInterrupt!\n");
  3647.   }
  3648.  
  3649.   abortExecution = true;
  3650.   exceptFlag = true;
  3651.   if (inCCode) {
  3652.     longjmp(cCalloutJmpBuf, 1);    /* throw out from C code */
  3653.   }
  3654. }
  3655.  
  3656.  
  3657. /*
  3658.  *    static signalType interruptHandler(sig)
  3659.  *
  3660.  * Description
  3661.  *
  3662.  *    Called to handle serious problems, such as segmentation violation.
  3663.  *    Tries to show a method invocation backtrace if possibly, otherwise
  3664.  *    tries to show where the system was in the file it was procesing when
  3665.  *    the error occurred.
  3666.  *
  3667.  * Inputs
  3668.  *
  3669.  *    sig   : Signal number, an integer
  3670.  *
  3671.  * Outputs
  3672.  *
  3673.  *    not used.  Always exits from Smalltalk.
  3674.  */
  3675. static signalType interruptHandler(sig)
  3676. int    sig;
  3677. {
  3678.   switch (sig) {
  3679.   case SIGBUS:
  3680.     errorf("Bus Error");
  3681.     break;
  3682.  
  3683.   case SIGSEGV:
  3684.     errorf("Segmentation violation");
  3685.     break;
  3686.     
  3687.   default:
  3688.     errorf("Unknown signal caught: %d", sig);
  3689.   }
  3690.  
  3691.   if (makeCoreFile) {
  3692.     kill(getpid(), SIGQUIT);    /* die with a core dump */
  3693.   }
  3694.  
  3695.   if (inInterpreter) {
  3696.     showBacktrace();
  3697.   } else {
  3698.     errorf("Not in interpreter!!");
  3699.   }
  3700.  
  3701.   exit(1);
  3702. }
  3703.  
  3704. static void showBacktrace()
  3705. {
  3706.   OOP        context, receiver, receiverClass;
  3707.   MethodContext methodContext;
  3708.  
  3709.   for (context = thisContextOOP; !isNil(context);
  3710.        context = methodContext->sender) {
  3711.     if (!isRealOOP(context)) {
  3712.       printf("Context stack corrupted!\n");
  3713.       break;
  3714.     }
  3715.     methodContext = (MethodContext)oopToObj(context);
  3716.     if (!isRealOOP(methodContext->selector)) {
  3717.       printf("Context stack corrupted!\n");
  3718.       break;
  3719.     }
  3720.       
  3721.     receiver = methodContext->receiver;
  3722.     if (isInt(receiver)) {
  3723.       receiverClass = integerClass;
  3724.     } else {
  3725.       if (!isRealOOP(receiver)) {
  3726.     printf("Context stack corrupted!\n");
  3727.     break;
  3728.       }
  3729.       receiverClass = oopClass(receiver);
  3730.     }
  3731.     printObject(receiverClass);
  3732.     printf(">>");
  3733.     printObject(methodContext->selector);
  3734.     printf("\n");
  3735.   }
  3736.  
  3737. }
  3738.  
  3739. static Boolean isRealOOP(oop)
  3740. OOP oop;
  3741. {
  3742.   return (oop >= oopTable && oop < &oopTable[TOTAL_OOP_TABLE_SLOTS]);
  3743. }
  3744.